Hi all,
I have a form on my program, and I have two comboboxes that I want to fill with data from columns in my database. I've tried three or four different ways that I've found searching around the web, but none of them seem to be working. They don't produce errors, but they don't load the combos either. For clarity, I've tried to bind them to the database, I've tried using datasets, and tried datatables. The code I've been working with is below. Can anyone offer a suggestion to get this working for me? Thanks!

con.Open()

'load hotel dropdown
Dim cmd As OleDbCommand = New OleDbCommand("SELECT Hotel FROM Hotels ORDER BY Hotel", con)
Dim dt As New DataTable()
Dim da As New OleDbDataAdapter()
da.SelectCommand = cmd
da.Fill(dt)

Dim r As DataRow
For Each r In dt.Rows
    cmb_hotel.Items.Add(r(1).ToString)
Next

'load restaurant dropdown
cmd = New OleDbCommand("SELECT Rest_Name FROM Restaurants ORDER BY Rest_Name", con)
Dim dt1 As New DataTable()
Dim da1 As New OleDbDataAdapter()
da1.SelectCommand = cmd
da1.Fill(dt)

Dim r1 As DataRow
For Each r1 In dt.Rows
    cmb_restaurant.Items.Add(r1(1).ToString)
Next
con.Close()

It may seem like alot of work, but this will pay off in the long run.

When working with databases, I always try to create strongly typed classes.

(Check the file attached for an example)

So you can do the following to retreive a list:

Dim hHotel as New Hotel
Dim lstHotel as New List(Of Hotel)

lstHotel = hHotel.SelectAll()

For i = 0 To lstHotel.Count - 1
    cmb_hotel.Items.Add(lstHotel(i).Hotel)
Next

Once you tackle classing the objects, the rest is SUPER easy.

Check if this will be fine for u

Dim cmd As New OleDbCommand("SELECT colname FROM Tablename", Connection)
Dim reader As OleDbDataReader = cmd.ExecuteReader
combobox1.Items.Clear()
While reader.Read
      combobox1.Items.Add(reader("colname"))
End While
reader.close()

Thanks for the suggestions, but for whatever reason, I still can't fill the combos. After poojavb's suggestion, the code I have is:

con.Open()
'load hotel dropdown
Dim cmd As OleDbCommand = New OleDbCommand("SELECT Hotel FROM Hotels ORDER BY Hotel", con)
rdr = cmd.ExecuteReader
cmb_hotel.Items.Clear()
While rdr.Read
    cmb_hotel.Items.Add(rdr("Hotel"))
End While

'load restaurant dropdown
cmd = New OleDbCommand("SELECT Rest_Name FROM Restaurants ORDER BY Rest_Name", con)
rdr = cmd.ExecuteReader
cmb_restaurant.Items.Clear()
While rdr.Read
    cmb_restaurant.Items.Add(rdr("Rest_Name"))
End While
con.Close()

I don't see any reason why this shouldn't work. I've double checked to make sure I don't inadvertantly clear the combos elsewhere in the code, but I'm okay there. I honestly don't know what else to try. Is there anything else I can try?

Debug ur code and check the flow of ur code...u will come to know where u are facing problem....by clicking F8

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.