Listbox not clearing

A

Angel Blue01

I have a form with a binding navigator that draws from a database via
drag-and-drop-created controls. I'm filling a listbox with the results
of a query. The listbox contains data related to the record that the
user is viewing but cannot be databound because other events and user
input are needed when the user selects an item from the listbox.

Each time the user clicks the binding navigator to move to the next
record I have this:

<code>
Private Sub VolunteersBindingSource_PositionChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
VolunteersBindingSource.PositionChanged
lboCurrentActivities.Items.Clear()

Call lboCurrentActivities.Refresh()

Dim cnnVol As OleDbConnection
'Dim cmdLineItemSelect As OleDbCommand

'get the activities for that volunteer in the listbox
Try
cnnVol = New OleDbConnection(strconn)

cnnVol.Open()
Dim strSQL As String = "SELECT lineitems.activitycode,
activitydescription, lineitems.newrenew, lineitems.batchnumber FROM
lineitems INNER JOIN activities on lineitems.activitycode =
activities.activitycode WHERE [vol#]=" &
VolunteersBindingSource.Position + 1 & ""


Dim testda = New OleDbDataAdapter(strSQL, cnnVol)

testda.Fill(testds, "li")

Dim testdt As New DataTable
testdt = testds.Tables(0)
Dim testdr As DataRow

If lboCurrentActivities.Items.Count = 0 Then
For Each testdr In testdt.Rows

lboCurrentActivities.Items.Add(testdr("activitydescription"))

Next
End If

' the newrenew textbox will be filled on listbox selction changed
based on the dataset

Catch ex As Exception
MessageBox.Show(ex, "Position changed")
End Try

End Sub
</code>

The listbox is supposed to clear each time the record advances but it
does not. The previous items stay in the listbox. The program insists
there are no items in the listbox and fills it with the new items
although the user can select and item from the supposedly cleared
items.

How do I get this listbox to clear?
 
G

Guest

Angel,

I think the datatable's Fill method is adding rows to the "li" table.

Then the For Each loop is adding back the old data to the listbox, along
with the new data.

Kerry Moorman
 
A

Angel Blue01

Angel,

I think the datatable's Fill method is adding rows to the "li" table.

Then the For Each loop is adding back the old data to the listbox, along
with the new data.

OK, then how do I delete that old data so it doesn't add it back?
 
G

Guest

Angel,

Maybe before calling the dataadapter's Fill method you could clear the table:

If testds.Tables.Count > 0 Then
testds.Tables(0).Clear()
End If

Kerry Moorman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top