Question : Set ID value in a Dynamically loaded Combo Box, VB.NET

I'm trying to get the id for a value selected in a combo box.  For example, I have a list of Manufacturers, each one has a name and an ID.  If the user selects the name, I would like the value returned to be the ID.

I've attached the code that loads the values into the combo box.  

This loads everything, but if I try to reteive the value using something like:  MsgBox("pMan: " & pMan.SelectedItem), I don't get the name, not the value of the ID.

I hope this make sense, i wasn't sure the best way to describe it.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
Private Sub FillMCbo(ByVal theSQL As String)
        Dim dr As DataRow
        Dim dt As DataTable
        Dim cmd As New OleDbCommand
        Dim ds As New DataSet
        Dim da As New OleDb.OleDbDataAdapter

        pMan.Items.Clear()
        sql = theSQL

        Try
            With da
                .TableMappings.Add("Table", "Category")
                cmd = New System.Data.OleDb.OleDbCommand(sql, con)
                .SelectCommand = cmd
                .Fill(ds)
                .Dispose()
            End With

            ds.AcceptChanges()
            dt = ds.Tables.Item(0)
            ds.Dispose()

            pMan.Text = ""
            pMan.Items.Clear()
            pMan.BeginUpdate()
            For Each dr In dt.Rows
                pMan.Items.Add(dr("ManName").ToString)
                pMan.ValueMember = dr("ManID")
            Next
            pMan.EndUpdate()
            pMan.SelectedIndex = 0
        Catch ex As Exception
            MsgBox("There was an error accessing Manufacterurs: " & vbCrLf & ex.Message, MsgBoxStyle.OkOnly, "Error!")
        End Try

    End Sub

Answer : Set ID value in a Dynamically loaded Combo Box, VB.NET

Once you make the changes then pMan.SelectedValue should return the correct value.
Random Solutions  
 
programming4us programming4us