I have been reading loads of questions and answers and even the .NET reference site. The code seems to be good but it's not working as expected.
I have a TextBox
where a barcode scanner is used to scan barcodes and put the number in it. Once the barcode is received it has to detect the Enter key and do a function that searches the database and add the item in a DataGridView
.
The code is working but it's only adding 1 item to the DataGridView
even if I scan multiple items.
Code:
Private Sub txtmodel_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtmodel.KeyPress
Dim purchasesource As New BindingSource
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
Dim query As String
query = "select prod_name as 'Product Name',prod_cost as 'Cost', prod_price as 'Price' from product_tbl where prod_STATUS = 'Active' and prod_code = '" & txtmodel.Text & "'"
cmd = New MySqlCommand(query, con)
da.SelectCommand = cmd
da.Fill(sql_get_purchase_item)
purchasesource.DataSource = sql_get_purchase_item
DataGridView1.DataSource = purchasesource
da.Update(sql_get_purchase_item)
MsgBox(sql_get_purchase_item.Rows.Count)
barcode = txtmodel.Text 'decaled in the class
TextBox1.Text &= barcode
txtmodel.Focus()
txtmodel.SelectAll()
End If
End Sub
Search that the scanned text is available in the database and add that row to the DataGridView
.
Try the below code:
Private Sub txtmodel_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtmodel.KeyPress
Dim purchasesource As New BindingSource
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
Dim query As String
query = "select prod_name as 'Product Name',prod_cost as 'Cost', prod_price as 'Price' from product_tbl where prod_STATUS = 'Active' and prod_code = '" & txtmodel.Text & "'"
cmd = New MySqlCommand(query, con)
da.SelectCommand = cmd
da.Fill(sql_get_purchase_item)
If sql_get_purchase_item.tables(0).rows.count > 0
For Each row As DataRow In sql_get_purchase_item.Rows
DataGridView1.rows.add(row.Item("Product Name"),row.Item("Cost"),row.Item("Price"))
Next row
End If
purchasesource.DataSource = sql_get_purchase_item
da.Update(sql_get_purchase_item)
txtmodel.Focus()
txtmodel.SelectAll()
End If
End Sub