Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I can fill the listbox with data from database, but I cannot refresh the listbox items after unbinding and rebinding. When I rebind the listbox1, it adds or accumulate all items and it does not refresh, it was just adding.

What I have tried:

This is the in filling listbox

<pre>  provider = "Provider=Microsoft.Ace.Oledb.12.0;"
               dbtblats = "Data Source =ATSDatabase.accdb;"
        tblatsconnection.ConnectionString = provider & dbtblats
        tblatsconnection.Open()
        SqlQuery = "SELECT * FROM ATS"
        datblats = New OleDb.OleDbDataAdapter(SqlQuery, tblatsconnection) '
        datblats.Fill(dstblats, "ATS")
        tblatsconnection.Close()

        Me.ListBox1.DataSource = dstblats.Tables("ATS")
        Me.ListBox1.DisplayMember = "FullNameCN"


I tried this code to unbind and rebind data with new data:

<pre> Listbox1.DataSource = Nothing
        provider = "Provider=Microsoft.Ace.Oledb.12.0;"
               dbtblats = "Data Source =ATSDatabase.accdb;"
        tblatsconnection.ConnectionString = provider & dbtblats
        tblatsconnection.Open()
        SqlQuery = "SELECT * FROM ATS WHERE DateProcessed IS NULL ORDER BY DateReceived DESC"
        datblats = New OleDb.OleDbDataAdapter(SqlQuery, tblatsconnection) '
        datblats.Fill(dstblats, "ATS")
        tblatsconnection.Close()
               Me.ListBox1.DataSource = dstblats.Tables("ATS")
        Me.ListBox1.DisplayMember = "FullNameCN"
Posted
Updated 11-Dec-18 22:31pm

1 solution

The DataAdapter.Fill Method (System.Data.Common) | Microsoft Docs[^] adds the new rows to the existing set. You need to clear your adapter tables first.
 
Share this answer
 
v2
Comments
kyrons 12-Dec-18 20:40pm    
Thank you Sir Richard MacCutchan. I can fill data in listbox but I cannot clear the existing data. I used listbox1.datasource = nothing and listbox1.items.clear but I got an error "Items collection cannot be modified when the DataSource property is set." can you give me the solution or any suggestion to clear the existing items in listbox?
Richard MacCutchan 13-Dec-18 3:48am    
You cannot use listbox1.items.clear on a bound control, since the items come from the binding. You need to remove the existing data from the source.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900