0

I have two columns in MS SQL table (ID, and Name) that I want to use to populate a list box with. I would like to show the Name values (as alias?) in the list box, however when the user selects the item I want the ID value to be returned not the Name value. My code below just adds the values into the list box from the Name column.

 Me.listName.Items.Clear()

 Dim strName As String  = "select SetName from " & tb

 Dim con As String = sConnectionString
 Dim com As New SqlCommand(strServiceType, New SqlConnection(con))
 com.Connection.Open()

 Dim dr As SqlDataReader
 Dim ColumnValue As String = Nothing

 dr = com.ExecuteReader
 While dr.Read
   ColumnValue = (dr.GetValue(0)).ToString
   listName.Items.Add(ColumnValue)
   listName.Sorted = True
 End While

 com.Connection.Close()

I'm not sure how to apply the logic above to get the associated ID value besides running another select statement on the list box SelectedIndexChanged event.

Thank you

1 Answer 1

2

If I'm not mistaken, you simply need to edit your sql to pull both ID and Name, then edit your addition of an item to your list box to add a new list item instead.

i.e.

listName.Items.Add(New ListItem("TEXT","VALUE"))
Sign up to request clarification or add additional context in comments.

2 Comments

where is ListItem coming from? Is it a list variable? My code complete does not show this option for my list box.
so each item in your list (listName) is and instance of the class ListItem, this simply creates one before adding it to the list itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.