troubleshooting Question

How to Get ID from ListBox (Win App)

Avatar of Mehawitchi
Mehawitchi asked on
.NET ProgrammingVisual Basic.NET
5 Comments1 Solution238 ViewsLast Modified:
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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros