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

4

1 回答 1

2

如果我没记错的话,您只需要编辑您的 sql 以提取 ID 和名称,然后编辑您在列表框中添加的项目以添加新的列表项。

IE

listName.Items.Add(New ListItem("TEXT","VALUE"))
于 2012-09-07T14:36:54.627 回答