Add two field from database to combobox
-
Hi I use this code and want to add id and avtor to combobox but display the field "avtor", when I select some from combobox I want to get "id"
Private Sub avtori()
Dim selectdata As String
selectdata = "SELECT id, avtor FROM avtor"
Dim strConn As String
Dim reader As OleDbDataReader
'Низ за връзка с БД
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strAcessFilePath1 & ";Jet OLEDB:Database Password=;"
OleDbConnection2 = New OleDbConnection(strConn)
OleDbConnection2.Open() 'Отваряне на връзка с БД
OleDbCommand2 = New OleDbCommand(selectdata, OleDbConnection2)
reader = OleDbCommand2.ExecuteReader()
'Цикъл за извличане на всички автори и тяхното добавя към комбинирина списъчна кутия
Dim i As Integer
While reader.Read()
ComboBox1.Items.Add(reader("avtor"))End While reader.Close() OleDbCommand2.Dispose() OleDbConnection2.Close() End Sub
modified on Saturday, February 5, 2011 2:15 PM
-
Hi I use this code and want to add id and avtor to combobox but display the field "avtor", when I select some from combobox I want to get "id"
Private Sub avtori()
Dim selectdata As String
selectdata = "SELECT id, avtor FROM avtor"
Dim strConn As String
Dim reader As OleDbDataReader
'Низ за връзка с БД
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strAcessFilePath1 & ";Jet OLEDB:Database Password=;"
OleDbConnection2 = New OleDbConnection(strConn)
OleDbConnection2.Open() 'Отваряне на връзка с БД
OleDbCommand2 = New OleDbCommand(selectdata, OleDbConnection2)
reader = OleDbCommand2.ExecuteReader()
'Цикъл за извличане на всички автори и тяхното добавя към комбинирина списъчна кутия
Dim i As Integer
While reader.Read()
ComboBox1.Items.Add(reader("avtor"))End While reader.Close() OleDbCommand2.Dispose() OleDbConnection2.Close() End Sub
modified on Saturday, February 5, 2011 2:15 PM
Instead of going through a datareader and adding each item to the combobox, fill a datatable with the data, then bind the combobox to it using its DataSource property. Then set the combos DisplayMember (what the user sees in the list) and the ValueMember (what is returned by the combo) properties to the column names in the datatable.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak