Delete MultipleRows of Database Using CheckBox in ASP.NET

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Configuration;
using
System.Data.SqlClient;
using
System.Data;
using
System.Collections.Specialized;
using
System.Text; 
using
System.Collections;
 

namespace
DeleteMultipleRows
{
    public partial class DeleteMultipleRows : System.Web.UI.Page
    {
        public string connStr = ConfigurationManager.ConnectionStrings["BRS"].ConnectionString;
 
        DataSet ds = new DataSet();
 
        string r;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                datas();
            }
        }
        private void datas()
        {
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand cmd = new SqlCommand("Select * from Tbl_BugReportingSystem", conn);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ad.Fill(ds, "Tbl_BugReportingSystem");
            GrdReport.DataSource = ds;
            GrdReport.DataBind();
        }
 
        protected void grdclient_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
 
        }
        protected void Grd_Download_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
               
//int id = Convert.ToInt16((e.Row.FindControl("lblBugId") as Label).Text);
 
               
//Functions objfn = new Functions();
               
//DataTable file = new DataTable();
 
               
//file = objfn.GetAFile(id);
               
//DataRow row = file.Rows[0];
 
               
//if (file.DefaultView[0]["AttachmentName"].ToString().Trim() == "")
               
//{
               
//    (e.Row.FindControl("Hyplnk_Attachments") as HyperLink).Visible = false;
               
//}
 
            }
        }
        protected void GrdReport_SelectedIndexChanged(object sender, EventArgs e)
        {
 
        }
 
        protected void btn1_Click(object sender, EventArgs e)
        {
            StringCollection sc = new StringCollection();
           
//for (int i = 0; i < GrdReport.Rows.Count; i++)
            foreach (GridViewRow gvrow in GrdReport.Rows)
            {
                CheckBox cb = (CheckBox)gvrow.FindControl("chk123");
//find the CheckBox
                if (cb != null)
                {
                    if (cb.Checked)
                    {
                        int usrid = Convert.ToInt32(GrdReport.DataKeys[gvrow.RowIndex].Value);
                       
//string id = GrdReport.Rows[i].Cells[1].Text;   
                        sc.Add(usrid.ToString());
                        DeleteRecords(sc);
                    }
                }
            }
            datas();
        }
        private void DeleteRecords(StringCollection sc)
        {
            SqlConnection conn = new SqlConnection(connStr);
           
//StringBuilder sb = new StringBuilder(string.Empty);          
           
try
            {
                conn.Open();
                foreach (string item in sc)
                {
 
                    SqlCommand cmd = new SqlCommand("delete from Tbl_BugReportingSystem where BugId=" + item, conn);
                    cmd.ExecuteNonQuery();
                }
            }
 
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Deletion Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
           
finally
            {
                conn.Close();
            }
        }
    }
}
 

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="DeleteMultipleRows.aspx.cs" Inherits="DeleteMultipleRows.DeleteMultipleRows"
%>
 

<
asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</
asp:Content>
<
asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div align
="center">
        <asp:GridView ID="GrdReport" runat="server" AutoGenerateColumns="False" Width
="50%"
            BackColor="White" CellPadding="4" ForeColor="Black" GridLines="Vertical" DataKeyNames
="BugId"
            OnRowDataBound="Grd_Download_RowDataBound" CssClass="BugGridview " OnSelectedIndexChanged
="GrdReport_SelectedIndexChanged">
            <AlternatingRowStyle BackColor="White"
/>
            <Columns
>
                <asp:TemplateField
>
                    <ItemTemplate
>
                        <asp:CheckBox ID="chk123" runat="server"
/>
                    </ItemTemplate
>
                </asp:TemplateField
>
                <asp:TemplateField HeaderText
="BugId">
                    <ItemTemplate
>
                        <asp:Label ID="lblBugId" runat="server" Text='<%#Bind("BugId")%>'></asp:Label
>
                    </ItemTemplate
>
                </asp:TemplateField
>
                <asp:TemplateField HeaderText
="BugName">
                    <ItemTemplate
>
                        <asp:Label ID="lblBugName" runat="server" Text='<%#Bind("BugName") %>'></asp:Label
>
                    </ItemTemplate
>
                </asp:TemplateField
>
                <asp:TemplateField HeaderText
="BugDescription">
                    <ItemTemplate
>
                        <asp:Label ID="lblBugDescription" runat="server" Text='<%#Bind("BugDescription") %>'></asp:Label
>
                    </ItemTemplate
>
                </asp:TemplateField
>
                <asp:TemplateField HeaderText
="Attachments">
                    <ItemTemplate
>
                        <%--   <asp:LinkButton ID="lbtnAttachments" runat="server" Text="Download" Font-Underline="true"></asp:LinkButton>--
%>
                        <asp:HyperLink ID="Hyplnk_Attachments" runat="server" Font-Underline="true" NavigateUrl='<%# Eval("BugId", "GetFile.aspx?ID={0}&Attachmentid=1") %>
'
                            Text="Download" ForeColor="Blue"></asp:HyperLink
>
                    </ItemTemplate
>
                </asp:TemplateField
>
                <asp:TemplateField HeaderText
="Status">
                    <ItemTemplate
>
                        <asp:LinkButton ID="lbtnStatus" runat="server" Text="View" Font-Underline
="true"
                            CommandName="Select"></asp:LinkButton
>
                    </ItemTemplate
>
                </asp:TemplateField
>
                <asp:TemplateField HeaderText
="Action">
                    <ItemTemplate
>
                        <asp:LinkButton ID="lbtnEdit" runat="server" Text="Edit" CommandName="Edit" Font-Underline="true"></asp:LinkButton
>
                    </ItemTemplate
>
                </asp:TemplateField
>
            </Columns
>
            <FooterStyle BackColor="#CCCC99"
/>
            <HeaderStyle CssClass="BugGridHeader "
/>
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right"
/>
            <RowStyle BackColor="#F7F7DE"
/>
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White"
/>
            <SortedAscendingCellStyle BackColor="#FBFBF2"
/>
            <SortedAscendingHeaderStyle BackColor="#848384"
/>
            <SortedDescendingCellStyle BackColor="#EAEAD3"
/>
            <SortedDescendingHeaderStyle BackColor="#575357"
/>
        </asp:GridView
>
    </div
>
    <table
>
        <tr
>
            <td
>
                <asp:Button ID="btn" runat="server" Text="delete" OnClick="btn1_Click"
/>
            </td
>
            <td
>
                <headertemplate> <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images.jpg"  Height="20px" Width
="24px"
                onclick="btn1_Click" /></headertemplate
>
            </td
>
        </tr
>
    </table
>
</
asp:Content>

Ebook Download
View all
Learn
View all