-
[2005] Combo ID Help
I have the following code to load my combo field. Can someone tell me how I could get the ID of a selected value in the combo and place it in a variable?
Code:
conn.Open()
Try
Dim Command As New OleDbCommand("SELECT * from tbl_StaffFerndown", conn) ' Find Connection
Dim dr As OleDb.OleDbDataReader = Command.ExecuteReader ' Read the data
cmbUsers.BeginUpdate() ' Begin Update of the combo
Do While dr.Read ' Read til no more entries
' Add ID and Username to classes and also in the combo
cmbUsers.Items.Add(New ListBoxItem(CInt(dr("UserID")), dr("UserName").ToString))
Loop
cmbUsers.EndUpdate() ' End the update
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "Error")
End Try
conn.Close()
-
Re: [2005] Combo ID Help
I would suggest changing the way you load the data. Create a DataTable and call its Load method, passing the DataReader as a parameter. You then bind the DataTable to the ComboBox via the DataSource property. Assign "UserName" to the DisplayMember property and "UserID" to the ValueMember and you're done. The ComboBox will now list the names and you get the ID of the selected item from the SelectedValue property.