-2

When I scan the barcode, it just shows me the one number barcode in the database only. My other long number barcodes no show out

private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                if ((e.KeyCode != Keys.Enter) || (textBox4.Text.Length == 0))
                {
                    return;
                }

                conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\Database\book1.mdf;Integrated Security=True;Connect Timeout=30");
                conn.Open();
                SqlDataAdapter adp = new SqlDataAdapter("SELECT productid,ProductName,Description,Stock,UOM,Price from ProductTable where productId='" + textBox4.Text + "'", conn);
                DataTable dt = new DataTable();
                adp.Fill(dt);

                foreach (DataRow item in dt.Rows)
                {
                    int i = dataGridView1.Rows.Add();
                    DataGridViewRow row = dataGridView1.Rows[i];
                    row.Cells[0].Value = item[0].ToString();
                    row.Cells[1].Value = item[1].ToString();
                    row.Cells[2].Value = item[2].ToString();
                    row.Cells[3].Value = item[3].ToString();
                    row.Cells[4].Value = item[4].ToString();
                    row.Cells[5].Value = item[5].ToString();
                }
                conn.Close();    
            }
            textBox4.Text=" ";
        }

Page Screenshots:

-https://ibb.co/xHsg198

LauTan
  • 23
  • 5
  • And what do you want to accomplish? – Alexander Jan 29 '19 at 13:07
  • i just want to scan many digit number barcode – LauTan Jan 29 '19 at 13:07
  • 1
    If you are using the KeyDown event - it could be that you get an event per character in the barcode. You could either wait until the terminating character is received (if any) from the barcode reader. Or maybe wait until the button is clicked. – PaulF Jan 29 '19 at 13:14

2 Answers2

2

Remove line

textBox4.Text=" ";

because it erases you barcode digits. And when barcode scan is finished and Keys.Enter is sent textbox contains only one digit.

If you need to clear your textbox after search put this line at the end of conditional statement

private void textBox4_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        //...
        conn.Close();
        textBox4.Text=" ";
    }
}
Alexander
  • 9,104
  • 1
  • 17
  • 41
  • if remove textBox4.Text=" "; the program will scan one item to datagrid only and it can't scan second item to it – LauTan Jan 29 '19 at 16:24
0

Just like @Alexander said, remove :

textBox4.Text = " ";

You can also change the event your catching to textBox4_Validated, which will happen only after leaving the textBox (You can leave the KeyDown, and only catch Enter key to force validation of the textBox).

If your goal is only digit number codes, you could add a test to verify if it's really only digits.

Another tip, if you can programm your scanner, at the end of the string scanned you should add a specific char (like '\n' or anything you know will never be in a real code). Like that you can scan all your codes in a row, then split the final string and execute the code in a foreach.

Last point, which is not really important here, when you catch a key press like here just to execute some code and you don't want that keyPress to be effectively added in your textBox or else, add this :

e.SuppressKeyPress = true;
e.Handled = true;

I'm not sure exactly what this does, but if I anderstood well, the first line will indicate that the keyPress become as if it never happened and the second tell the system that the keyPress have been correctly used and Handled, so it don't have to interract with it anymore.

Hope this helped

Zoma
  • 321
  • 7
  • 20