title: Delete Record using GridView
body: <p>I have some problem in delete record. I use VB.net with Access Database. When I try to run this program there have no error but the record didn't delete in database. There are one class to delete record and this class will called another method which is call DeleteMultipleRecords. Here is my code, hope anyone can help me to solve this.</p>
  
  <p><strong>-Default.aspx.vb-</strong></p>
  
  <pre><code>Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
      'create string collection to store IDs of records to be deleted
      Dim idCollection As New StringCollection()
      Dim strID As String = String.Empty
      'Loop through GridView rows to find checked rows 
      For i As Integer = 0 To i &lt; GridView1.Rows.Count - 1
          Dim chkDelete As CheckBox = DirectCast(GridView1.Rows(i).Cells(0).FindControl("chkSelect"), CheckBox)
          If chkDelete IsNot Nothing Then
              If chkDelete.Checked Then
  
                  strID = GridView1.Rows(i).Cells(1).Text
                  idCollection.Add(strID)
  
              End If
          End If
      Next
  
      'called method to delete record
      DeleteMultipleRecords(idCollection)
  
      'rebind(GridView)
      GridView1.DataBind()
  End Sub
  Private Sub DeleteMultipleRecords(ByVal idCollection As StringCollection)
      'create connection
      Dim cnnOLEDB As New OleDbConnection(strConnection)
      Dim IDs As String = ""
  
      'create string builder to store 
      'delete commands seperated by ;
  
      For Each id As String In idCollection
  
          IDs += id.ToString() &amp; ","
      Next
  
      Try
  
          Dim strIDs As String = IDs.Substring(0, IDs.LastIndexOf(""))
          Dim strSql As String = ("Delete from Details WHERE ID = '" &amp; strIDs &amp; "' ")
  
          cmdOLEDB.CommandType = CommandType.Text
          cmdOLEDB.CommandText = strSql
          cmdOLEDB.Connection = cnnOLEDB
          cnnOLEDB.Open()
          cmdOLEDB.ExecuteNonQuery()
          cmdOLEDB.Dispose()
  
      Catch ex As OleDbException
  
          Dim errorMsg As String = "Error in Deletion"
          errorMsg += ex.Message
          Throw New Exception(errorMsg)
  
      Finally
          cnnOLEDB.Close()
      End Try
  
  End Sub
  </code></pre>
  
  <p><strong>-Default.aspx-</strong></p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
          DataSourceID="SqlDataSource1" AutoGenerateColumns="False" DataKeyNames="ID"&gt;
          &lt;Columns&gt;
          &lt;asp:TemplateField HeaderText="Select"&gt;
  &lt;ItemTemplate&gt;
  
  &lt;asp:CheckBox ID="chkSelect" runat="server" /&gt;
  &lt;/ItemTemplate&gt;
   &lt;/asp:TemplateField&gt;
  &lt;asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID"/&gt;
  
  &lt;asp:TemplateField HeaderText="Name" 
                     SortExpression="Name"&gt;
  &lt;ItemTemplate&gt;
  &lt;asp:TextBox ID="txtName" runat="server" 
               Text='&lt;%# Bind("Name") %&gt;'  ReadOnly="true" 
               BorderStyle="none" 
               BorderWidth="0px" &gt;
  &lt;/asp:TextBox&gt;
  &lt;/ItemTemplate&gt;
  &lt;/asp:TemplateField&gt;
  
  &lt;asp:TemplateField HeaderText="Location" 
                     SortExpression="Location"&gt;
  &lt;ItemTemplate&gt;
  &lt;asp:TextBox ID="txtLocation" runat="server" 
               Text='&lt;%# Bind("Location") %&gt;' 
               ReadOnly="true" 
               BorderStyle="none" BorderWidth="0px"&gt;
  &lt;/asp:TextBox&gt;
  &lt;/ItemTemplate&gt;
  &lt;/asp:TemplateField&gt;
          &lt;/Columns&gt;
      &lt;/asp:GridView&gt;
  
      &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" 
          ConnectionString="&lt;%$ ConnectionStrings:LabSystemDBConnectionString %&gt;" 
          ProviderName="&lt;%$ ConnectionStrings:LabSystemDBConnectionString.ProviderName %&gt;" 
          SelectCommand="SELECT * FROM [Details]"
          DeleteCommand = "DELETE FROM [Details] WHERE ID = [@ID]"&gt;
  
      &lt;DeleteParameters&gt;
      &lt;asp:Parameter Name="ID" /&gt;
      &lt;/DeleteParameters&gt;&lt;/asp:SqlDataSource&gt;
  
  
      &lt;asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" OnClientClick="return DeleteConfirmation();"  Text="Delete" /&gt;
      &amp;nbsp;&lt;br /&gt;
  </code></pre>