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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hello
You mean like this:
Dim x As Integer
x = 99
ComboBox1.Items.Add("USA" & " - " & x.ToString)
ComboBox Result:
-------------------------- ------
USA - 99 |V|
-------------------------- ------
?
:)
You mean like this:
Dim x As Integer
x = 99
ComboBox1.Items.Add("USA" & " - " & x.ToString)
ComboBox Result:
--------------------------
USA - 99 |V|
--------------------------
?
:)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Idle Mind. This is exactly what I wanted
ASKER
I will still use this loop to populate my combo
Do While drSQL.Read()
objListItem = New ListItem(drSQL.Item("Marke
cbMarket.Items.Add(objList
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