bgernon
asked on
Retrieve checkedlistbox values into a comma deliniated string
I've been struggling to retrieve the values of the checked items from a checkedlistbox. I need to place the values into a comma delineated string to use in a sql query. I'd done a similar thing with a datagridview. I tried to apply the same logic, with no success. I thought I had found the answer at:
https://www.experts-exchange.com/questions/21143011/How-to-retrieve-checked-values-from-databound-checkedlistbox-windows-vb-net.html
The line IDString &= chkOperID.SelectedValue & "," gives me an error: Variable 'IDString' is used before it has been assigned a value. A null reference exception could result at runtime.
Help!
https://www.experts-exchange.com/questions/21143011/How-to-retrieve-checked-values-from-databound-checkedlistbox-windows-vb-net.html
The line IDString &= chkOperID.SelectedValue & "," gives me an error: Variable 'IDString' is used before it has been assigned a value. A null reference exception could result at runtime.
Help!
ASKER
Tried that already. All I get is a string of commas.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry? This question is still open.
Anyway, we're getting closer. I now have a comma delineated string. Just have to put the parentheses at the beginning and end. I think I know how. Will play with it some more.
Anyway, we're getting closer. I now have a comma delineated string. Just have to put the parentheses at the beginning and end. I think I know how. Will play with it some more.
ASKER
Success!
Dim IDString As String = "("
Dim i As Integer
For Each i In chkOperID.CheckedIndices
chkOperID.SelectedIndex = i
IDString &= chkOperID.Items(i) & ","
Next
IDString = IDString.Substring(0, IDString.LastIndexOf(",")) + ")"
A messagebox shows the string I needed to place in my query.
Thanks CodeCruiser.
Dim IDString As String = "("
Dim i As Integer
For Each i In chkOperID.CheckedIndices
chkOperID.SelectedIndex = i
IDString &= chkOperID.Items(i) & ","
Next
IDString = IDString.Substring(0, IDString.LastIndexOf(","))
A messagebox shows the string I needed to place in my query.
Thanks CodeCruiser.
ASKER
Thanks
ASKER
I did not need ' chkOperID.SelectedIndex = i'. I commented it out.
Glad to help :-)
Dim IDString As String = ""