locked
Picking up data to auto fill RRS feed

  • Question

  • I am not sure if I am in the right place, but I am confident that someone here can help me.  I have a database of over 1000 customers and 15 vendors.  Currently each time I type in the customer name I have to manually look up their customer number.  Is there a way to type the customer name in one field and have the customer number autofill in a different field.  I have done a search and looked through the website, but maybe I am using the wrong terminology and it not looking for the right thing.  Any help would be appreciated.
    Tuesday, July 1, 2008 8:40 PM

Answers

  • You can deal with the Name_TextBox's KeyPressed Event. When "EnterKey" is pressed it means Ending input the Name_TextBox. And you can use a sql statement to query the number of which name you has input. Then bind the searched result to the number field.

    Wednesday, July 2, 2008 7:10 AM
  • agree with zamesking's answer, handle the textBox.KeyDown event and write the code such as:

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    this.textBox1.Tag = GetIDByName(this.textBox1.Text);  // put the id in the textBox1.tag
                }
            }

            private int GetIDByName(string name)
            {
                string sql = " select ID from employee where name = " + name;  // sql select command
                string connString = "";          // your database connection string
                DataTable dt = new DataTable();
                SqlConnection sqlConn = new SqlConnection(connString);
                SqlDataAdapter sqlDA = new SqlDataAdapter(sql, sqlConn);
                try
                {
                    sqlConn.Open();
                    sqlDA.Fill(dt);
                    sqlConn.Close();
                }
                catch
                {
                }

                return Convert.ToInt32(dt.Rows[0][0]);
            }

    Wednesday, July 2, 2008 7:50 AM

All replies

  • You can deal with the Name_TextBox's KeyPressed Event. When "EnterKey" is pressed it means Ending input the Name_TextBox. And you can use a sql statement to query the number of which name you has input. Then bind the searched result to the number field.

    Wednesday, July 2, 2008 7:10 AM
  • agree with zamesking's answer, handle the textBox.KeyDown event and write the code such as:

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    this.textBox1.Tag = GetIDByName(this.textBox1.Text);  // put the id in the textBox1.tag
                }
            }

            private int GetIDByName(string name)
            {
                string sql = " select ID from employee where name = " + name;  // sql select command
                string connString = "";          // your database connection string
                DataTable dt = new DataTable();
                SqlConnection sqlConn = new SqlConnection(connString);
                SqlDataAdapter sqlDA = new SqlDataAdapter(sql, sqlConn);
                try
                {
                    sqlConn.Open();
                    sqlDA.Fill(dt);
                    sqlConn.Close();
                }
                catch
                {
                }

                return Convert.ToInt32(dt.Rows[0][0]);
            }

    Wednesday, July 2, 2008 7:50 AM