Link to home
Start Free TrialLog in
Avatar of Mehawitchi
Mehawitchi

asked on

How to Get ID from ListBox (Win App)

Hello Experts,

I have this code which populates a combo box with list of market names (String) and market ID (Integer).

When this code runs, you will only see the market name in the combo box. My question is what is the command (or code) for exposing the Market ID. In other words, if a user slected "USA" as a market name from the drop down, how can I get the Market ID of USA from the combo box itself, rather than getting it from the original database (Select MarketID FROM myTable WHERE MarketName ='USA'

Thank you
Private Sub PopulateMarketCombo()
        ' This procedure populates the market combo box
        Dim cnSQL As SqlConnection
        Dim cmSQL As SqlCommand
        Dim drSQL As SqlDataReader
        Dim strSQL As String
        Dim objListItem As ListItem
 
        Try
            strSQL = "SELECT MarketID, MarketName FROM Wave.Market ORDER BY MarketID"
            cnSQL = New SqlConnection(cnString)
            cnSQL.Open()
            cmSQL = New SqlCommand(strSQL, cnSQL)
            drSQL = cmSQL.ExecuteReader()
            cbMarket.Items.Clear()
            Do While drSQL.Read()
                objListItem = New ListItem(drSQL.Item("MarketName").ToString(), CInt(drSQL.Item("MarketID")))
                cbMarket.Items.Add(objListItem)
            Loop
 
            drSQL.Close()
            cnSQL.Close()
            cmSQL.Dispose()
            cnSQL.Dispose()
 
        Catch e As SqlException
            MsgBox(e.Message, MsgBoxStyle.Critical, "SQL Error")
 
        Catch e As Exception
            MsgBox(e.Message, MsgBoxStyle.Critical, "General Error")
        End Try
    End Sub

Open in new window

SOLUTION
Avatar of rionroc
rionroc
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mehawitchi
Mehawitchi

ASKER

Thanks for your reply rionroc, but I think I didn't explain my problem very well.

I will still use this loop to populate my combo

Do While drSQL.Read()
                objListItem = New ListItem(drSQL.Item("MarketName").ToString(), CInt(drSQL.Item("MarketID")))
                cbMarket.Items.Add(objListItem)
            Loop
After populating the combo, I will disconnect from the SQL server and then show the form with the combo box.
Once the user selects a specific market from the combo box, I need to get the related MarketID, from the combo itself, since each MarketID was stored along with the MarketName in the combo. Otherwise, what is the use of storing the MarketID in the first place.

I need to know what is the VB.Net code that would list the MarketID from the combo, based on the selecteditem.

I hope it is clearer now

Thanks again for your help
Hello


You mean like this:

Dim x As Integer
x = 99
ComboBox1.Items.Add("USA" & " - " & x.ToString)


ComboBox Result:
--------------------------------
USA - 99                      |V|
--------------------------------
?



:)
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Idle Mind. This is exactly what I wanted