Build string for SQL statement with multiple checkboxes in vb.net
<p>I am currently working with a SQL back end and vb.net Windows Forms front end. I am trying to pull a report from SQL based on a list of checkboxes the user will select. </p>

<p>To do this I am going to use an <code>IN</code> clause in SQL. The only problem is if I use an if statement in vb.net to build the string its going to be a HUGE amount of code to set up the string. </p>

<p>I was hoping someone knew a better way to do this. The code example below shows only selecting line 1 and selecting both line 1 and 2. I will need for the code to be able to select any assortment of the lines. The string will have to be the line number with a comma following the number. This way when I include the code in my SQL query it will not bug. </p>

<p>Here is the code:</p>

<pre><code>Dim LineString As String
    'String set up for line pull
    If CBLine1.Checked = False And CBLine2.Checked = False And CBLine3.Checked = False And CBLine4.Checked = False And _
         CBLine7.Checked = False And CBLine8.Checked = False And CBLine10.Checked = False And CBLine11.Checked = False And CBLine12.Checked = False Then
        MsgBox("No lines selected for download, please select lines for report.")
        Exit Sub
    End If

    If CBLine1.Checked = True And CBLine2.Checked = False And CBLine3.Checked = False And CBLine4.Checked = False And _
        CBLine7.Checked = False And CBLine8.Checked = False And CBLine10.Checked = False And CBLine11.Checked = False And CBLine12.Checked = False Then
        MsgBox("This will save the string as only line 1")

    ElseIf CBLine1.Checked = True And CBLine2.Checked = True And CBLine3.Checked = False And CBLine4.Checked = False And _
    CBLine7.Checked = False And CBLine8.Checked = False And CBLine10.Checked = False And CBLine11.Checked = False And CBLine12.Checked = False Then
        MsgBox("This will save the string as only line 1 and 2")
    End If
</code></pre>

<p>The final string will have to be inserted into a SQL statement that looks like this:</p>

<pre><code>SELECT * 
FROM tabl1 
WHERE LineNumber IN (-vb.netString-)
</code></pre>

<p>The above code will need commas added in for the string.</p>