Clearing a Listbox

dgorka

Well-known member
Joined
Dec 27, 2006
Messages
88
Programming Experience
5-10
Okay, So I have a Listbox that contains information from a Database. What I'm doing is selecting rows in that listbox, and then updating the database information for those rows. Then it should refresh the listbox and redraw it without those items that were selected. I thought I had this working before, but when I came back to it today, it didn't work. Here is my code for loading the listbox. This is called at the form load and after the database is updated.

VB.NET:
Private Sub LoadListBox()
        Me.lstMovies.DataSource = Nothing
        Me.lstMovies.Items.Clear()

        conn.ConnectionString = My.Settings.gcstrConnection

        strSQL = "SELECT Movie_ID, Title FROM Movies WHERE Status = 'out';"

        cmd = conn.CreateCommand
        cmd.CommandText = strSQL
        conn.Open()
        da.SelectCommand = cmd
        Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(da)
        da.Fill(dsMovies, "Movies")
        conn.Close()
        conn.Dispose()
        cmd.Dispose()

        Me.lstMovies.DataSource = dsMovies.Tables("Movies")
        Me.lstMovies.DisplayMember = "Title"
        Me.lstMovies.ValueMember = "Movie_ID"

        Me.lstMovies.Refresh()
        My.Application.DoEvents()
    End Sub

However, once this code executes the items remain in the listbox, even though the database has been updated. Any ideas?
 
I don't understand. What is supposed to keep the items that were selected from being repopulated?

By the way, setting the DataSource to nothing, clearing the items, reassigning the DataSource, refreshing the listbox, and calling DoEvents are all surpurflous.
With a databound control, if you change the DataSource, the control automatically updates to show the changes.
 
Youre using .NET 2.0 and making your data access life a headache. Take a read of the DW2 link in my signature, section on creating a form to search data. It guides the proper way to make a parameterized search query. Once you have a listbox on your form and bound to the database properly, all you will need is one line of code to refresh the contents
 
Back
Top