Results 1 to 3 of 3

Thread: How to delay execution of code in textBox3_TextChanged which is used for barcode ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2018
    Posts
    123

    How to delay execution of code in textBox3_TextChanged which is used for barcode ?

    Hi
    I have textBox3_TextChanged
    used to let user search database by entering barcode in it
    but when user try to do that , only the first number enters .So how to make this to delay until user finish entering the whole barcode into textBox3.text
    Code:
      private void textBox3_TextChanged(object sender, EventArgs e)
      {
          SqlConnection con = new SqlConnection
          (@"Data Source=(LocalDB)\MSSQLLocalDB;
          AttachDbFilename=D:\repos\FastSales\FastSales\Database1.mdf;
          Integrated Security=True");
          try
          {
              if (textBox1.Text != "")
              {
                  con.Open();
                  SqlCommand cmd = new SqlCommand("select * from item where barcode =  '" + textBox1.Text + "'", con);
                  SqlDataReader dr = cmd.ExecuteReader();
                  dr.Read();
                  textBox4.Text = dr[0].ToString();
                  textBox6.Text = dr[1].ToString();
                  textBox7.Text = dr[2].ToString();
                  textBox8.Text = dr[3].ToString();
                  textBox9.Text = dr[4].ToString();
                  textBox10.Text = dr[5].ToString();
    
                  dr.Close();
                  DataRow row = dtsales.NewRow();
    
                  row[0] = textBox4.Text;
                  row[1] = textBox6.Text;
                  row[2] = textBox7.Text;
                  row[3] = textBox8.Text;
                  row[4] = textBox9.Text;
                  row[5] = textBox10.Text;
    
                  dtsales.Rows.Add(row);
    
                  textBox1.SelectAll();
                  textBox1.Focus();
    
                  decimal Total = 0;
                  for (int i = 0; i < dataGridView1.Rows.Count; i++)
                  {
                      Total += Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);
                  }
                  textBox5.Text = Total.ToString();
                  textBox1.Text = "";
                  textBox2.Text = "0.0";
    
                  textBox1.Focus();
                  return;
              }
              else if (textBox10.Text != "")
              {
                  con.Open();
                  SqlCommand cmd = new SqlCommand("select * from item where barcode =  '" + textBox10.Text + "'", con);
                  SqlDataReader dr = cmd.ExecuteReader();
                  dr.Read();
                  textBox4.Text = dr[0].ToString();
                  textBox6.Text = dr[1].ToString();
                  textBox7.Text = dr[2].ToString();
                  textBox8.Text = dr[3].ToString();
                  textBox9.Text = dr[4].ToString();
                  textBox10.Text = dr[5].ToString();
    
                  dr.Close();
                  DataRow row = dtsales.NewRow();
    
                  row[0] = textBox4.Text;
                  row[1] = textBox6.Text;
                  row[2] = textBox7.Text;
                  row[3] = textBox8.Text;
                  row[4] = textBox9.Text;
                  row[5] = textBox10.Text;
    
                  dtsales.Rows.Add(row);
    
                  textBox1.SelectAll();
                  textBox1.Focus();
    
                  decimal Total = 0;
                  for (int i = 0; i < dataGridView1.Rows.Count; i++)
                  {
                      Total += Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);
                  }
                  textBox5.Text = Total.ToString();
                  textBox1.Text = "";
                  textBox2.Text = "0.0";
    
                  textBox1.Focus();
              }
              else
              {
                  return;
              }
          }
    
          catch (Exception ex) { MessageBox.Show(ex.Message); }
    
      }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,532

    Re: How to delay execution of code in textBox3_TextChanged which is used for barcode

    How EXACTLY do you know that the user has finished entering the barcode? Is it because they have entered a specific number of characters? In that case, it should be obvious that you need to check for that number of characters first. If it's something else, it should be obvious that you need to check for that first. At the very least, it should be obvious that you need to determine what the actual trigger should be and check for that and, if you need help, tell us what it is.

    This seems like a problem that you haven't really thought about too much. It seems like you have jumped into writing code before thinking about the logic that that code is actually supposed to implement. The logic part doesn't actually require any programming experience at all, so it should be the easy part.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,222

    Re: How to delay execution of code in textBox3_TextChanged which is used for barcode

    Yeah, certainly that, but there's even more. The way you have it, the search it performed as soon as the first character is entered, which is really unlikely to be right. As soon as that first character is entered, then you go ahead as if it was completely correct and complete. You query the DB for that (in an utterly unsafe fashion that leaves your code open to SQL injection attacks, by the way), and just assume that it is correct. After all, you call dr.Read, and ignore the return from that. If that returns False, then there were no rows retrieved. What then happens when you try to read the row that was not returned? Probably just an exception, which is pretty bad, because there is nothing exceptional about that.

    But the first thing you need to do is what JMC stated: Figure out how to know when the user is done entering stuff. Usually, this is done using a button, but it could also be done based on length, or based on whether or not anything was returned (though the performance of that might not be acceptable).
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width