DK2014
DK2014

Reputation: 181

Populate column values from MS Access database into VB .Net Combo-Box dropdown values?

I am accessing data from Access MDB file using my application developed in VB .net 2010. I want to get values from a particular table-column to be populated in a Combo Box dropdown.

I followed a similar thread @ C# Adding data to combo-box from MS Access table but as the code is in C#, I was not able to successfully implement it. And reverted back to my previous code.

Because the Table will be updated by another application as well, every time I use my utility I need to get the latest data from that table. Hence I would like to have a feature that could get all available id from table.

Below is what I have been trying to do..

'below declared at class level. 
' Dim cnnOLEDB As New OleDbConnection
' Dim cmdOLEDB As New OleDbCommand

cnnOLEDB.Open()

cmdOLEDB.CommandText = "select unique_id from tag_data"
cmdOLEDB.Connection = cnnOLEDB

Dim rdrOLEDB2 As OleDbDataReader = cmdOLEDB.ExecuteReader

If rdrOLEDB2.Read = True Then
    TagComboBox1.DataSource = rdrOLEDB2.Item(0).ToString

    TagComboBox1.ValueMember = "unique_id"
    TagComboBox1.DisplayMember = "unique_id"

End If
rdrOLEDB2.Dispose()
rdrOLEDB2.Close()
cnnOLEDB.Close()

Upvotes: 2

Views: 7360

Answers (1)

Trevor
Trevor

Reputation: 8004

I would recommend filling a DataTable with your query. Then you can set the DataSource of the combobox, ValueMember & the DisplayMember.

 Dim dt As New DataTable
 Dim query As String = "select unique_id from tag_data"

 Using connection As New OleDbConnection("your connection string")
  Using command As New OleDbCommand(query, connection)
    Using adapter As New OleDbDataAdapter(command)
        connection.Open()
        adapter.Fill(dt)
        connection.Close()
    End Using
  End Using
 End Using

 If dt.Rows.Count > 0 Then
  TagComboBox1.DataSource = dt
  TagComboBox1.ValueMember = "unique_id"
  TagComboBox1.DisplayMember = "unique_id"
 End If

Upvotes: 2

Related Questions