Results 1 to 2 of 2

Thread: [2005] Combo ID Help

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    Bournemouth
    Posts
    63

    [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()

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width