1

In a WinForm C# application I have a Barcode_textbox. In TextChanged event I have an SQL query to check for the TextBox.Text value in the database which is the barcode. The problem is that for each entered digit the SQL Query will be fired, so if a barcode of length 13 it will make 13 check in the database and it is being extremely SLOW.

So, what is the TextBox event that is fired only when the user stops writing in the TextBox (or the barcode reader stpos reading), or what is the optimal solution for checking for a barcode in the database. Note that bacode is of different length

fadd
  • 584
  • 4
  • 16
  • 41
  • 1
    Adding a button with "Check validity" text is not an option? – Steve Apr 09 '13 at 22:51
  • Apart from Steves suggestion, you could listen for the `Enter` key? – Patrick Apr 09 '13 at 22:53
  • 1
    this is not optimal, imagine this software in a supermarket. It will be time consuming to click on a button each time you reader from a barcode reader! – fadd Apr 09 '13 at 22:54
  • How many barcode records get queried each time? If the count is small (e.g. a few hundred or thousand) you could query ALL the records once and store them in a local array. This would allow you to query as often as you'd like because it would be very fast. We use this strategy for our "Country" text box but that only has a little over 200 records. – Jesse Webb Apr 09 '13 at 22:54
  • Do you have any other constraints since you mention supermarket. Is it a touch screen for instance, or are devices such as keyboard and mouse available to the device that runs your application? – Patrick Apr 09 '13 at 23:03
  • Most barcode readers put a carriage return at the end of the scanned code. You can check this in the `KeyUp` event. If the keycode is `13`, submit the query. – Simon Whitehead Apr 09 '13 at 23:06

5 Answers5

2

I recall how I did this with success.

I put Timer control in my application with a Interval of a second (1000 milli's). Then I set the Form.KeyPreview property to True.

In the Form Key Press event I store the key strokes. In the Timer.Tick event check if the length of the recorded key strokes exceeds 12 or so characters.

Then fire off the call to SQL, once. When you return a record successfully (or if the textbox contains greater than say 20 chars) clear the stored key strokes.


See update, as at March 2019: https://stackoverflow.com/a/55411255/495455

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 1
    An addition is to cancel the timer and start it in the textchanged event. That way the timer will trigger "1 second after the user stops typing". This would of course mean the user would have to type at least one character each second in order for the sql query not to fire. – Patrick Apr 09 '13 at 23:00
  • 1
    Why why why?! Timers are such a terrible solution to this. – Simon Whitehead Apr 09 '13 at 23:13
  • Because it was VB6. I want to disclaim the use of a Timer, the pearl here is the Form.KeyPreview trick. – Jeremy Thompson Apr 09 '13 at 23:21
2

Timers are a horrible solution to this.

Use the KeyUp event of the TextBox and check for a carriage return. Most barcode scanners submit a carriage return after the code.. and if they don't do it by default, they come with barcodes to configure it to do so.

You can test this by opening Notepad and scanning barcode after barcode into it. Each one will be on a new line.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
1

Use timer in this fashion that when user stops typing in your textbox for a given small time say 1 second only then get data from database...

Also you can place a check at the characters typed by user with a counter such that when it exceeds that minimum(the minimum size that your key can be) get the data...Will save much time

Wajahat
  • 1
  • 6
0

You could use the Validating event to check the content of the TextBox.
Your user will be required to press the TAB key to change the current focus from the TextBox to the next control following the taborder and the validating event will be triggered

private void textBox_BarCode_Validating(object sender, CancelEventArgs e)
{
     // Code to check the barcode on the SQL database....       
     if(!ValidBarCode(textBox1.Text))
     {
           // Cancel the event and select the text to be corrected by the user.
           e.Cancel = true;
           textBox1.Select(0, textBox1.Text.Length);
     }

}

Suppose that the next control is the command that executes something using the Barcode inserted (Add)(or a textBox to insert the quantity of the item described by the barcode). After a while all the operation becomes a very natural data entry processing

Steve
  • 213,761
  • 22
  • 232
  • 286
0

What about doing the query async?

If you can't or want an easy dirty solution, then I would just stick to the timer

Amegon
  • 629
  • 6
  • 15