简体   繁体   中英

database values in textbox

If the voucher number that I typed exists in the table, it should show the details in the respective textboxes, but if it doesn't exist a message box that says (ID doesn't exists! ) would show.

For example, the voucher number 101 exists in the table, First,I would type '1' in the textbox , the messagebox would immediately appear ... Second I would continue the number after clicking ok it will now be number "10" a messagebox will again appear that says (ID doesn't exists! ). Then finally I would be able to type "101" the details would already show in the respective textboxes.

My problem is that when everytime that I typed a single number, a messagebox that says (ID doesn't exists! ) appears. How do I solve that?

textchanged property of "textBox22" code:

   private void textBox22_TextChanged(object sender, EventArgs e)
    {
        String path = "Data Source=LOCALHOST; Initial Catalog= sadd; username=root; password=''";
        MySqlConnection sqlconn = new MySqlConnection(path); //communicator //constructors
        MySqlCommand sqlcomm = new MySqlCommand();
        MySqlDataReader sqldr;
        sqlconn.Open();
        sqlcomm.Connection = sqlconn;
        sqlcomm.CommandType = CommandType.Text;
        sqlcomm.CommandText = "Select * from approvedrecords where VoucherNumber=" + textBox22.Text + "";

        sqldr = sqlcomm.ExecuteReader();
        sqldr.Read();

        if (sqldr.HasRows)
        {
            textBox26.Text = sqldr[0].ToString();

        }
        sqlconn.Close();


        if (textBox22.Text == textBox26.Text)
        {

            String path8 = "Data Source=LOCALHOST; Initial Catalog= sadd; username=root; password=''";
            MySqlConnection sqlcon = new MySqlConnection(path8); //communicator //constructors

            string query = "select * from approvedrecords where VoucherNumber = " + textBox22.Text + " ";
            MySqlCommand cmd = new MySqlCommand(query, sqlcon);
            MySqlDataReader dbr;


            sqlcon.Open();
            dbr = cmd.ExecuteReader();
            while (dbr.Read())
            {

                string a = (string)dbr["CheckNumber"].ToString();
                string b = (string)dbr["DateCreated"];
                string c = (string)dbr["Status"];
                string d = (string)dbr["PayeesName"];
                string f = (string)dbr["Amount"].ToString();
                string g = (string)dbr["DatePrinted"];
                string h = (string)dbr["Particulars"];
                string i = (string)dbr["Prepared_by"];
                string j = (string)dbr["Payment_received_by"];

                textBox21.Text = a;
                textBox23.Text = b;
                textBox28.Text = c;
                textBox20.Text = d;
                textBox19.Text = f;
                textBox27.Text = g;
                textBox18.Text = h;
                textBox16.Text = i;
                textBox17.Text = j;

            }
        }
        else
        {
            MessageBox.Show("ID doesn't exist!");
        }

Why don't you try using the OnLostFocus event of the textbox? As explained here . That way your code would be called only when the user abandons your textbox.

Alternatively, if you want to keep your OnTextChanged handler, I would recommend using async calls my adding an UpdatePanel as explained here . You would need to add a label showing the status of the query every time the user typed in a character, so when no data is returned by your query, the label would show "No results found for this ID" and no textboxes would be populated. When results are found, then the label would read "Data was found for this ID" and you would populate the controls accordingly.

I hope this helps and I hope I was clear :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM