Delete Multiple Records From ASP.Net GridView Using CheckBoxes

The following is the table design from where I am fetching employee records.

employee records
                                             Image 1.

  1. CREATE TABLE [dbo].[Employee](  
  2.     [Emp_ID] [int] IDENTITY(1,1) NOT NULL,  
  3.     [Name] [varchar](50) NULL,  
  4.     [Email] [varchar](500) NULL,  
  5.     [Designation] [varchar](50) NULL,  
  6.     [City] [varchar](50) NULL,  
  7.     [State] [varchar](50) NULL,  
  8.     [Country] [varchar](50) NULL,  
  9.  CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED   
  10. (  
  11.     [Emp_ID] ASC  
  12. )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ONON [PRIMARY]  
  13. ON [PRIMARY]  
  14.   
  15. GO 

The following is the data in my table:

table
                                                                         Image 2.

Now my aspx is:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>Delete Multiple Records In Grid View Using Check Box in ASP.NET C#</title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.         <table style="border: solid 15px blue; width: 100%; vertical-align: central;">  
  12.             <tr>  
  13.                 <td style="padding-left: 50px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue;  
  14.                     font-size: 20pt; color: orangered;">  
  15.                     Delete Multiple Records In Grid View Using Check Box in ASP.NET C#  
  16.                 </td>  
  17.             </tr>  
  18.             <tr>  
  19.                 <td style="text-align: left; padding-left: 50px; border: solid 1px red;">  
  20.                     <asp:GridView ID="GridViewEmployee" runat="server" DataKeyNames="Emp_ID" AutoGenerateColumns="False"  
  21.                         Width="90%" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"  
  22.                         CellPadding="4" GridLines="Both">  
  23.                         <Columns>  
  24.                             <asp:TemplateField HeaderText="Select">  
  25.                                 <ItemTemplate>  
  26.                                     <asp:CheckBox ID="chkSelect" runat="server" />  
  27.                                 </ItemTemplate>  
  28.                             </asp:TemplateField>  
  29.                             <asp:BoundField DataField="Name" HeaderText="Employee Name" />  
  30.                             <asp:BoundField DataField="Email" HeaderText="Email" />  
  31.                             <asp:BoundField DataField="Designation" HeaderText="Designation" />  
  32.                             <asp:BoundField DataField="City" HeaderText="City" />  
  33.                             <asp:BoundField DataField="State" HeaderText="State" />  
  34.                             <asp:BoundField DataField="Country" HeaderText="Country" />  
  35.                         </Columns>  
  36.                         <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />  
  37.                         <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />  
  38.                         <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />  
  39.                         <RowStyle BackColor="White" ForeColor="#003399" />  
  40.                         <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />  
  41.                     </asp:GridView>  
  42.                 </td>  
  43.             </tr>  
  44.             <tr>  
  45.                 <td align="right">  
  46.                     <asp:Button ID="btnDeleteRecords" Text="Delete Records" OnClick="btnDeleteRecords_Click"  
  47.                         runat="server" />  
  48.                 </td>  
  49.             </tr>  
  50.         </table>  
  51.     </div>  
  52.     </form>  
  53. </body>  
  54. </html> 

My aspx.cs code is:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9.   
  10. public partial class _Default : System.Web.UI.Page  
  11. {  
  12.     SqlDataAdapter da;  
  13.     DataSet ds = new DataSet();  
  14.     DataTable dt = new DataTable();  
  15.   
  16.     protected void Page_Load(object sender, EventArgs e)  
  17.     {  
  18.         if (!Page.IsPostBack)  
  19.             this.BindGrid();  
  20.     }  
  21.   
  22.     private void BindGrid()  
  23.     {  
  24.         SqlConnection con = new SqlConnection();  
  25.         ds = new DataSet();  
  26.         con.ConnectionString = @"Data Source=INDIA\MSSQLServer2k8; Initial Catalog=EmployeeManagement; Uid=sa; pwd=india;";  
  27.         SqlCommand cmd = new SqlCommand("SELECT * FROM EMPLOYEE", con);  
  28.   
  29.         da = new SqlDataAdapter(cmd);  
  30.         da.Fill(ds);  
  31.         con.Open();  
  32.         cmd.ExecuteNonQuery();  
  33.         con.Close();  
  34.   
  35.         if (ds.Tables[0].Rows.Count > 0)  
  36.         {  
  37.             GridViewEmployee.DataSource = ds.Tables[0];  
  38.             GridViewEmployee.DataBind();  
  39.         }  
  40.     }  
  41.   
  42.     protected void btnDeleteRecords_Click(object sender, EventArgs e)  
  43.     {  
  44.         DataTable dt = new DataTable();  
  45.         dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name"typeof(string)),  
  46.                         new DataColumn("Email",typeof(string)) });  
  47.   
  48.   
  49.         foreach (GridViewRow row in GridViewEmployee.Rows)  
  50.         {  
  51.             if ((row.FindControl("chkSelect"as CheckBox).Checked)  
  52.             {  
  53.                 int Emp_ID = Convert.ToInt32(GridViewEmployee.DataKeys[row.RowIndex].Value);  
  54.                 using (SqlConnection con = new SqlConnection(@"Data Source=INDIA\MSSQLServer2k8; Initial Catalog=EmployeeManagement; Uid=sa; pwd=india;"))  
  55.                 {  
  56.                     con.Open();  
  57.                     SqlCommand cmd = new SqlCommand("DELETE FROM Employee WHERE Emp_ID=" + Emp_ID, con);  
  58.                     cmd.ExecuteNonQuery();  
  59.                     con.Close();  
  60.                 }  
  61.             }  
  62.         }  
  63.   
  64.         this.BindGrid();  
  65.     }  

Now run the application:

run
                                                                     Image 3

Now select some records using the checkboxes and click on the "Delete Records" button.

delet record
                                                                     Image 4.

output
                                                                  Image 5.

Up Next
    Ebook Download
    View all
    Learn
    View all