0

Help! I am trying to fill a listbox with items from my database with the sql statement, I'm unsure how to fix this error as it keeps coming up. My database only contains two dates, but I need it to be expandable in future, hence the database to store dates.

Public ds As New DataSet 
Public con As New OleDb.OleDbConnection 'used to connect to the database
Public provider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
Public datafile As String = "Resources/database.accdb" 'database location and version
Public da As OleDb.OleDbDataAdapter
Public sqlstatement As String


ds.Clear()
    con.ConnectionString = connString
    con.Open()
    sqlstatement = "SELECT ShowDate FROM AvailableDates"
    da = New OleDb.OleDbDataAdapter(sqlstatement, con)
    da.Fill(ds, "Dates")

    lbxDates.DisplayMember = "ShowDate"
    lbxDates.DataSource = ds
    lbxDates.ValueMember = "ShowDate"
    con.Close()

The error 'Cannot bind to the new display member' occurs on the

lbxDates.ValueMember = "ShowDate"   line
chandler
  • 13
  • 7
  • way you use my answer in your code and you don't mark the answer how soled your issue ? http://stackoverflow.com/questions/37187867/error-object-reference-not-set-to-an-instance-of-an-object#37187867 – Beldi Anouar May 12 '16 at 13:18
  • Im sorry, how do i do that? – chandler May 12 '16 at 13:21

1 Answers1

2

You must affect the dataSource first :

  lbxDates.DataSource = ds.tables("Dates")
  lbxDates.DisplayMember = "ShowDate"
    lbxDates.ValueMember = "ShowDate"
Beldi Anouar
  • 2,170
  • 1
  • 12
  • 17