abhishek kulkarni
abhishek kulkarni

Reputation: 39

Barcode not scanned properly in textbox c#

I have a barcode scanning application which runs on text change event of text box. it verifies if the barcode is previously scanned into access database and authenticates user accordigngly.

I have a problem that the number seen in textbox is only a single digit(first digit) of the barcode read.

Is this problem because the text change property is immediately validated ?

code is as following

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        textBox1.SelectAll();
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\dell\Documents\Database5.accdb;Jet OLEDB:Database Password=sudeep;");
        con.Open();
        try
        {
        //    con.Open();
             OleDbCommand cmd = new OleDbCommand("insert into barcode(id,dtime) values('" + textBox1.Text + "','"+label3.Text+"')",con);
            cmd.ExecuteNonQuery();
            label2.Text = "User Authenticated";
            label2.ForeColor = Color.Green;
            //MessageBox.Show("User Authenticated");

        }

        catch(Exception)
        {
            label2.Text = "User Already registered";
            label2.ForeColor = Color.Red;
           // MessageBox.Show(x.ToString());


        }

Please help.

I think the problem is that the text box does not scan the the string of characters of barcode as due to text change event property only one character is scanned and stored in the database. Is there a way to scan string of characters from barcode ?

Upvotes: 0

Views: 1227

Answers (2)

ChrisLively
ChrisLively

Reputation: 88044

You don't want to hook into keypress because you'll run into the issue you just saw.

Instead you need to find out what else the scanner sends across. Most of the time scanners send a CR/LF after the data. This allows you to have a button on your form that you designate as the default button to accept the "enter" key and run your logic off of it's onclick event.

And by "most" I mean pretty much every scanner you will find on the market that wasn't custom built to NOT do this.

Upvotes: 2

C.Evenhuis
C.Evenhuis

Reputation: 26436

Usually these barcode scanners send keyboard events and indeed cause the event to be raised for each digit separately.

You should see if the barcode scanner terminates the scan with a separate key, for instance the enter key, and catch the OnKeyDown event instead. Alternatively, if the barcode is of a fixed length, you could check the length of the TextBox.Text property before acting on it.

Upvotes: 2

Related Questions