468,321 Members | 1,797 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,321 developers. It's quick & easy.

Listbox not clearing

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("activitydes cription"))

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?

Aug 14 '07 #1
3 1756
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
"Angel Blue01" wrote:
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("activitydes cription"))

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?

Aug 14 '07 #2
On Aug 14, 1:02 pm, Kerry Moorman
<KerryMoor...@discussions.microsoft.comwrote:
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?
Aug 15 '07 #3
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
"Angel Blue01" wrote:
On Aug 14, 1:02 pm, Kerry Moorman
<KerryMoor...@discussions.microsoft.comwrote:
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?
Aug 15 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by gv | last post: by
2 posts views Thread by amber | last post: by
5 posts views Thread by heddy | last post: by
1 post views Thread by WhiteWizard | last post: by
8 posts views Thread by PeteOlcott | last post: by
15 posts views Thread by Doogie | last post: by
1 post views Thread by howard w | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.