• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 566
  • Last Modified:

VB.NET clear data binded listbox items doesn't work

I am at a loss. The button_click below is supposed to clear the items of a listbox, which is bound to a dataset, and then rebind based on a new set of parameters.   I really thought the:
lbbuyer.Datasource = Nothing statement would have  worked.

The result with the button push is simply that the new items simply get added on to the old ones.
Please Help

Private Sub btnGetBuyers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetBuyers.Click 
lbBuyer.DataSource = Nothing
        Dim sqlcmd As New SqlCommand
        Dim strState As String = GetSelections(lbState)
        Dim strFund As String = GetSelections(lbFund)
        With sqlcmd
            .Connection = conString
            .CommandType = CommandType.StoredProcedure
            .CommandText = "SP_GETBUYERS"
            .CommandTimeout = 1000
            .Parameters.Add("@State", SqlDbType.Char, 3)
            .Parameters.Add("@Fund", SqlDbType.Char, 20)
            .Parameters("@State").Direction = ParameterDirection.Input
            .Parameters("@Fund").Direction = ParameterDirection.Input
            .Parameters("@State").Value = strState
            .Parameters("@Fund").Value = strFund
        End With
        Dim daBuyer As New SqlDataAdapter("SELECT DISTINCT CounterpartyName FROM tblInvBuyers", conString)
        daBuyer.Fill(dsInvoice, "Buyer")
        lbBuyer.DataSource = dsInvoice.Tables("Buyer")
        With lbBuyer
            .DataSource = dsInvoice.Tables("Buyer")
            .DisplayMember = "CounterpartyName"
        End With

Open in new window

1 Solution
Is the dsInvoice declared on the form ?
If so, I think you forgot to empty it before making another daBuyer.Fill(dsInvoice, "Buyer"). Try first doing:

If dsBnvoice,Tables.Contains("Buyer") Then dsInvoice.Tables("Buyer").rows.clear

Or delete the table from dsInvoice, or dispose it and make a new one before filling it.

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now