[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Retrieve checkedlistbox values into a comma deliniated string

Posted on 2012-08-31
8
Medium Priority
?
684 Views
Last Modified: 2012-08-31
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:

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_21143011.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!
0
Comment
Question by:bgernon
  • 5
  • 3
8 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38355359
That's a warning not an error. You can simply initialize it where you declare it

Dim IDString As String = ""
0
 

Author Comment

by:bgernon
ID: 38355397
Tried that already.  All I get is a string of commas.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 38355411
Change

IDString &= CheckedListBox1.SelectedValue & ","

to

IDString &= CheckedListBox1.Items(i) & ","


Did you not test the solution before closing the question?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:bgernon
ID: 38355443
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.
0
 

Author Comment

by:bgernon
ID: 38355463
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.
0
 

Author Closing Comment

by:bgernon
ID: 38355467
Thanks
0
 

Author Comment

by:bgernon
ID: 38355489
I did not need  ' chkOperID.SelectedIndex = i'.  I commented it out.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38355752
Glad to help :-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question