[RESOLVED] How to display item not found message box with this code?
I am trying to display a message box if the string entered in the textbox was not found in the database
Also, if the enter button is pressed and the textbox is blank, then a msgbox should say "A value must be entered"
I have tried everything to my knowledge and vb.net does not like it.
Code:
Private Sub ButtonLookup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLookup.Click
Try
Dbconnect.Open() ' Try Block - Connects To The Database. Displays An Error If It Can't.
Dim DbAdapter As New MySqlDataAdapter("SELECT * FROM Itemlist WHERE Barcode = '" & TextBoxItemInput.Text & "'", Dbconnect)
Dim DbDataSet As New DataSet
DbAdapter.Fill(DbDataSet, "Itemlist")
Dim DbTable As DataTable = DbDataSet.Tables("Itemlist")
For Each row As DataRow In DbTable.Rows
Dim ItemSku As Integer = row("ItemSku").ToString()
Dim ItemName As String = row("ItemName").ToString()
Dim Barcode As String = row("Barcode").ToString()
Dim Price As Decimal = row("Price").ToString()
ItemGridView.Rows.Add(Barcode, ItemName, 1, Price, Price)
LabelStatus.Text = "Item Added"
LineDisplay.ClearText()
LineDisplay.DisplayText(ItemName & " $" & Price)
ItemGridView.ClearSelection()
TextBoxItemInput.Clear()
Next
Re: How to display item not found message box with this code?
Fill will return the number of records retrieve, so you simply test whether that's zero or not. By the way, why create a DataSet if all you want is a DataTable? Why not just create a DataTable?
Code:
If myDataAdapter.Fill(myDataTable) = 0 Then
'No matching rows found.
Else
'Yes matching rows found.
End If
Re: How to display item not found message box with this code?
What does this mean. This is coming from the next statement.
"Collection was modified; enumeration operation might not execute."
I put it at the top as well, But it is add in a dgv row twice
Re: How to display item not found message box with this code?
A For Each loop requires that the list that you enumerate is not altered until the loop ends.
Re: How to display item not found message box with this code?
I just remembered , that maybe I should put in an Exit For , then then it could go on to the next thing.
It seems to work fine displaying a message box for "Item Found", when I type an actual number that is in the database
But when I type an invalid item number, it never shows the message box for "Item Not Found".
Re: How to display item not found message box with this code?
I'm ok now. I just had to organize the code into the right place.
Thank you, jmcilhinney
All is well
Re: How to display item not found message box with this code?
Perhaps you could show us the actual code you're talking about.