0

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!");
        }
  • wouldn't be better to connect to database once and store all info in a class. you are comparing first record in database only. you need to loop it with exit if it finds a match. I don't work with sql but this doesn't seems right if (sqldr.HasRows) { textBox26.Text = sqldr[0].ToString(); } – Claudius Mar 18 '16 at 11:50
  • You really should get into the habit of giving sensible names to your fields/properties/controls. Also you should use paramaterised sql queries as the way you are currently doing it is open to sql injection. – Steve Mar 18 '16 at 12:19

1 Answers1

0

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 :-)

Community
  • 1
  • 1
Hugo Nava Kopp
  • 2,906
  • 2
  • 23
  • 41