Results 1 to 7 of 7

Thread: [RESOLVED] How to display item not found message box with this code?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2012
    Location
    California
    Posts
    133

    Resolved [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2012
    Location
    California
    Posts
    133

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2012
    Location
    California
    Posts
    133

    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".
    Last edited by jcharles12; May 2nd, 2013 at 08:35 PM.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2012
    Location
    California
    Posts
    133

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to display item not found message box with this code?

    Perhaps you could show us the actual code you're talking about.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width