Search code examples
asp.netlistboxasp.net-2.0listbox-control

How do I get all the values in a listbox when looping through them?


I'm currently trying to move through all the values added to a listbox by the user, however, I want to retrieve the actual value of each item in the listbox and not the text.

I've gotten so far with the code below, but that only gets the text and not the value.

For Each item In SelectedStoresLB.Items
            Dim tCompany As Integer = CInt(Left(item.ToString, 1))
            Dim tStore As String = Right(item.ToString, 3)
            Dim tReason As String = ReasonTxt.Text
            insertSQL = "INSERT INTO [CommsDownLog] ([DimCompanyID],[PervasiveStoreNumber],[DownReason]) VALUES (" & tCompany & ", '" & tStore & "', '" & tReason & "')"
            Dim insertRow = New SqlCommand(insertSQL, objConn)
            Try
                objConn.Open()
                insertRow.ExecuteNonQuery()
                objConn.Close()
            Catch ex As Exception
                Response.Write(ex)
            End Try
        Next

How would I go about getting the value for each item in the collection?


Solution

  • item is a ListItem object - rather than call ToString on it, you should use the Text and Value properties to get the info you need.