Question Filling the combobox?

The

Member
Joined
Nov 27, 2014
Messages
16
Programming Experience
3-5
I built a new form. There is combobox that I want to fill from database
VB.NET:
            sqlquery.CommandText = "SELECT * FROM Buildings"
            Dim rdrOLEDB As OleDb.OleDbDataReader = sqlquery.ExecuteReader

            While rdrOLEDB.Read
                Dim B_Name = rdrOLEDB.GetString("id")
                Buildings_List.Items.Add(B_Name)
            End While
Now that line: Dim B_Name = rdrOLEDB.GetString("id")
will bring me one field only, but I want both the name and the id field. One for display, and the other would be the value that I will use as a reference for the combobox controller.
Any idea how to do this in VB.Net 2013??
 
you just fill the combobox into while function. like this
While rdrOLEDB.Read

Dim B_Name = rdrOLEDB.GetString("id")
ComboBox1.Items.Add(B_Name)
Buildings_List.Items.Add(B_Name)
End While

do you want like this? you mean filling the combobox, right?

Erwin Harry
 
you just fill the combobox into while function. like this
While rdrOLEDB.Read

Dim B_Name = rdrOLEDB.GetString("id")
ComboBox1.Items.Add(B_Name)
Buildings_List.Items.Add(B_Name)
End While

do you want like this? you mean filling the combobox, right?

Erwin Harry
Thanks Erwin
but that will give me a drop-list with the names only. I want to show the names to the user, but when submit the form , I want the ID (from the db table) of the selected choice should be available when I refere to the combobox in my sql insert query.
 
You should be populating a DataTable and binding that to the ComboBox, e.g.
Dim table As New DataTable

Using connection As New OleDbConnection("connection string here"),
      command As New OleDbCommand("SELECT ID, Name FROM MyTable", connection)
    connection.Open()

    Using reader = command.ExecuteReader()
        table.Load(reader)
    End Using
End Using

With Me.ComboBox1
    .DisplayMember = "Name"
    .ValueMember = "ID"
    .DataSource = table
End With
 
Back
Top