Hi,

I have a form where i can enter new product. In that same form i have listbox where i show the current product from the database.

I want to know how to update that listbox as i enter new product into the database so that user can see the current content of the product table.

my code to load the listbox

Try
            Dim ds As New DataSet
            Dim cmd As New OleDbCommand
            Dim da As OleDbDataAdapter = Nothing

            cmd.Connection = con
            cmd.CommandText = "Select ProductID,ProductName From products"

            da = New OleDbDataAdapter(cmd)
            da.Fill(ds, "product")

            da.Dispose()
            cmd.Dispose()

            With ListProduct
                .DataSource = ds.Tables("product") 'Binds the table to your listbox
                .DisplayMember = "ProductName" 'The col in the tbl you want to display
                .ValueMember = "ProductID"
                .SelectedIndex = -1
            End With

        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        Finally
            con.Close()

        End Try

My add new product code

Try

            Dim cmd As OleDbCommand
            Dim sqlstr As String, ans As Boolean
            
            sqlstr = "INSERT INTO Products(ProductName,ProductDescription) VALUES ('" & LTrim(txtP.Text) & "', '" & LTrim(txtDesc.Text) & "') "

            cmd = New OleDbCommand(sqlstr, con)
            ans = cmd.ExecuteNonQuery()

            If (ans = True) Then
                'message added
                MsgBox("success")
            Else
                MsgBox("not entered")   '' error
            End If
            cmd = Nothing

        Catch ex As Exception
            MessageBox.Show(ex.Message & " - " & ex.Source)
        End Try

To sum it up, what i want is something that will refresh the current display of the listbox so user can see the actual and updated contents of the database.

with the above code, the newly entered product/data can only be seen when the program is closed and reopened again. Which is not very convenient.
thanks

Place the code you use to load the listbox in a sub by itself and call it after inserting a product in the db

Place the code you use to load the listbox in a sub by itself and call it after inserting a product in the db

Thank you so much :) it worked perfectly.

This question has already been answered. Start a new discussion instead.