deletion of record in gridview using checkbox(multiple row deletion)in aspx.cs page:-
-------------------------------------------------------------------------------------------------------using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Specialized;
public partial class _Default : System.Web.UI.Page
{
SqlCommand cmd;
SqlDataAdapter da;
SqlConnection con = new SqlConnection("data source=RAJAT\\SQLEXPRESS;initial catalog= master;integrated security=sspi;");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
StringCollection idCollection = new StringCollection();
string strID = string.Empty;
for (int i = 0; i < Gridview1.Rows.Count; i++)
{
CheckBox Checkbox1 = (CheckBox)
Gridview1.Rows[i].Cells[0].FindControl("Checkbox1");
if (Checkbox1 != null)
{
if (Checkbox1.Checked)
{
strID = Gridview1.Rows[i].Cells[1].Text;
idCollection.Add(strID);
}
}
}
DeleteMultipleRecords(idCollection);
Gridview1.DataBind();
}
private void DeleteMultipleRecords(StringCollection idCollection)
{
//Create sql Connection and Sql Command
//con.Open();
SqlCommand cmd = new SqlCommand();
string IDs = "";
foreach (string id in idCollection)
{
IDs += id.ToString() + ",";
}
try
{
string strIDs =
IDs.Substring(0, IDs.LastIndexOf(","));
foreach (string id in idCollection)
{
string strSql = ("Delete from aparna WHERE id in ('" + id + "')");
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSql;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
catch (SqlException ex)
{
string errorMsg = "Error in Deletion";
errorMsg += ex.Message;
throw new Exception(errorMsg);
}
finally
{
con.Close();
}
}
}
code in .aspx page :-
---------------------------------------<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div>
<asp:GridView ID="Gridview1" runat ="server" AutoGenerateColumns ="false"
DataSourceID ="sqldatasource1" ShowFooter ="true"
onselectedindexchanged="Gridview1_SelectedIndexChanged">
<Columns >
<asp:TemplateField >
<ItemTemplate >
<asp:CheckBox ID="Checkbox1" runat ="server" />
</ItemTemplate>
<FooterTemplate >
<asp:Button ID="Button1" runat ="server" Text ="DELETE" OnClick="Button1_Click" OnClientClick ="return DeleteConfirmation();" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField ="id" HeaderText ="ID" />
<asp:BoundField DataField ="name" HeaderText ="NAME" />
<asp:BoundField DataField ="location" HeaderText ="LOCATION" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID ="sqldatasource1" runat ="server" ConnectionString ="Data Source=RAJAT\SQLEXPRESS;Initial Catalog=master;Integrated Security=True" SelectCommand ="select [id],[name],[location] from [aparna]">
</asp:SqlDataSource>
</div>
</asp:Content>
code in master page(site master page);-
-----------------------------------------------------------------------<head runat="server">
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
<script type="text/javascript" language="javascript">
function DeleteConfirmation()
{
if (confirm("Are you sure you want to delete selected records ?")==true)
return true;
else
return false;
}
</script>
</head>
No comments:
Post a Comment