I have a checklistbox in my windows form. I am querying data from database into sqldatareader. How do I check the checklistbox if the value is a string concatenated by comma.
Example, my data is as below.
Apple, Orange, Banana
I would like it to check the following:
The below code only add the list to my checklistbox instead of checking the boxes like the above.
Dim dr As SqlDataReader
Dim strQuery As String = "SELECT NAME WHERE ID = '" & strID & "'"
Dim myCon As New SqlConnection(strCon)
Dim myCom As New SqlCommand(strQuery, myCon)
myCom.CommandTimeout = 500
myCon.Open()
dr = myCom.ExecuteReader(CommandBehavior.CloseConnection)
While dr.Read
If drProject.HasRows Then
chklstFruit.BeginUpdate()
chklstFruit.Items.Add(dr.Item("NAME"))
chklstFruit.EndUpdate()
End If
End While
dim selected
selected = "Apple, Orange, Banana"
if (InStr(selected,"Orange ") then
' check the box
end if
That is an easy option. Except if the choices are Apples, Oranges" you can see that looking for the singular would still work.dim selected
selected = "Apple, Orange, Banana"
if find("Apple", selected) = 1 then
'we found a match check the box
end if
Function find(needle, haystack)
result = 0 ' set result to not found
ArrayHaystack = split(haystack,",") ' put comma delimited in an array
for each item in ArrayHaystack ' look through array
if item = needle then
result = 1 ' found
end if
next
find = result
End Function
You can do something similar by creating a dictionary and looking up the key https://www.tutorialspoint.com/vbscript/vbscript_dictionary_objects.htm
Dim dr As SqlDataReader
Dim strQuery As String = "SELECT NAME WHERE ID = '" & strID & "'"
Dim myCon As New SqlConnection(strCon)
Dim myCom As New SqlCommand(strQuery, myCon)
myCom.CommandTimeout = 500
myCon.Open()
dr = myCom.ExecuteReader(CommandBehavior.CloseConnection)
Dim Fruits = CStr(dr.Item("NAME").ToString)
For i As Integer = 0 To chklstFruit.Items.Count - 1
Dim item = (CStr(chklstFruit.Items(i)))
chklstFruit.SetItemChecked(i, item = Fruits)
Next
Dim dr As SqlDataReader
Dim strQuery As String = "SELECT NAME WHERE ID = '" & strID & "'"
Dim myCon As New SqlConnection(strCon)
Dim myCom As New SqlCommand(strQuery, myCon)
myCom.CommandTimeout = 500
myCon.Open()
dr = myCom.ExecuteReader(CommandBehavior.CloseConnection)
Dim Fruits = CStr(dr.Item("NAME").ToString)
Dim words As String() = Fruits.Split(New Char(){","c}).Select((Function(s) s.Trim)).ToArray()
For i As Integer = 0 To chklstFruit.Items.Count - 1
For Each word in words
Dim item = (CStr(chklstFruit.Items(i)))
chklstFruit.SetItemChecked(i, item = Fruits)
Next
Next
Dim dr As SqlDataReader
Dim strQuery As String = "SELECT NAME WHERE ID = '" & strID & "'"
Dim myCon As New SqlConnection(strCon)
Dim myCom As New SqlCommand(strQuery, myCon)
myCom.CommandTimeout = 500
myCon.Open()
dr = myCom.ExecuteReader(CommandBehavior.CloseConnection)
Dim Fruits = CStr(dr.Item("NAME").ToString)
Dim words As String() = Fruits.Split(New Char(){","c}).Select((Function(s) s.Trim)).ToArray()
For i As Integer = 0 To chklstFruit.Items.Count - 1
For Each word in words
Dim item = (CStr(chklstFruit.Items(i)))
If item = word then
chklstFruit.SetItemChecked(i, item = Fruits)
End If
Next
Next