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

Posted on 2009-02-24
Last Modified: 2012-05-06
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

Question by:suicehockey44
    LVL 10

    Accepted 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.
    LVL 4

    Expert Comment


    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Zoom web page in 2 21
    Specific format 21 109
    A macro to Count the number of rows across all worksheets 3 47
    Wordmerge 8 33
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now