3 Replies - 260 Views - Last Post: 13 December 2018 - 02:33 AM Rate Topic: -----

#1 dpointer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 06-March 09

How to clear and bind listbox with new data from database in VB.

Posted 12 December 2018 - 12:50 AM

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.

This is code the in filling listbox1:

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 but it just copying and accumulating items:

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"


Is This A Good Question/Topic? 0
  • +

Replies To: How to clear and bind listbox with new data from database in VB.

#2 andrewsw   User is online

  • dependency injected
  • member icon

Reputation: 6668
  • View blog
  • Posts: 27,315
  • Joined: 12-December 12

Re: How to clear and bind listbox with new data from database in VB.

Posted 12 December 2018 - 02:39 AM

I haven't looked at WinForms for a while (which I assume this is about) but, on a search, having set the DataSource to Nothing, try using the DataSourceChanged event handler to use Items.Clear. This suggests that setting the DataSource to Nothing just removes the connection but doesn't clear existing items.
Was This Post Helpful? 0
  • +
  • -

#3 dpointer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 06-March 09

Re: How to clear and bind listbox with new data from database in VB.

Posted 12 December 2018 - 08:28 PM

Thank you for suggestion. 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?

This post has been edited by andrewsw: 13 December 2018 - 02:32 AM
Reason for edit:: removed previous quote, just use the Reply button

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • dependency injected
  • member icon

Reputation: 6668
  • View blog
  • Posts: 27,315
  • Joined: 12-December 12

Re: How to clear and bind listbox with new data from database in VB.

Posted 13 December 2018 - 02:33 AM

Note that there is no need to quote a previous post (especially your own), there is a Reply button further down the page.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1