title: Best Way to Select Multiple Records GridView Asp.Net
body: <pre><code>Private Sub GetSelectedBasketRecord()
  Dim j As Integer = 0
  
  ViewState("SelectedBasket") = Nothing
  For i As Integer = 0 To gvSapBaskets.Rows.Count - 1
  
      Dim rb As CheckBox = DirectCast(gvSapBaskets.Rows(i).Cells(0).FindControl("CHKButton1"), CheckBox)
      If rb IsNot Nothing Then
          If rb.Checked Then
  
              Dim hf As HiddenField = DirectCast(gvSapBaskets.Rows(i).Cells(0).FindControl("HiddenFieldBasketID"), HiddenField)
              If hf IsNot Nothing Then
                  j = j + 1
                  If j = 1 Then
                      ViewState("SelectedBasket") = "PositionID = " &amp; hf.Value &amp; " "
                  Else
                      ViewState("SelectedBasket") = ViewState("SelectedBasket") &amp; "AND PositionID = " &amp; hf.Value &amp; " "
                  End If
                  BasketIDLBL.Text = ViewState("SelectedBasket")
              End If
  
              If i = gvSapBaskets.Rows.Count - 1 Then
                  Exit For
              End If
          End If
      End If
  
  Next
  End Sub
  </code></pre>
  
  <p>I have checkboxes in a gridview, </p>
  
  <ul>
  <li>a user can select multiple check boxes</li>
  <li>Each checkbox has an ID assigned to it</li>
  </ul>
  
  <p>Currently What i was planning to do was create an SQL query to bring back rows depending on what the user selects in the gridview, the viewstate would generate part of the SQL query</p>
  
  <ul>
  <li>if the user selects 1 row: ViewState("SelectedBasket") is <code>PositionID = 1234</code></li>
  <li>if 2 rows are selected ViewState("SelectedBasket") is <code>PositionID = 1234 AND PositionID = 5678</code></li>
  </ul>
  
  <p>Then i was going to generate SQL like</p>
  
  <p><code>"SELECT * FROM TABLE WHERE " &amp; ViewState("SelectedBasket") &amp; "</code></p>
  
  <p>ViewState("SelectedBasket") would be something like <code>PositionID = 1234 AND PositionID = 5678</code></p>
  
  <p>I have a feeling there is probably a better way around this, just cannot think of it</p>
  
  <p>Any help is much appreciated!</p>