Ans:-
Default.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ChkBx wth GrdVw Operations</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<asp:gridview ID="Gridview1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="cid">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Bind("cid") %>'></asp:Label>
</ItemTemplate>
<%--<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("cid") %>'></asp:TextBox>
</EditItemTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="cname">
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%# Bind("cname") %>'></asp:Label>
</ItemTemplate>
<%-- <EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("cname") %>'></asp:TextBox>
</EditItemTemplate>--%>
</asp:TemplateField>
</Columns>
</asp:gridview>
<br />
<asp:Button ID="btnDelete" runat="server" Text="Delete"
onclick="btnDelete_Click" />
</center>
</div>
</form>
</body>
</html>
Default.aspx.cs:-
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
static SqlConnection cn;
static SqlCommand cmd;
static SqlDataAdapter da;
static DataTable dt;
static string connStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetDatatoGv();
}
}
void GetDatatoGv()
{
cn = new SqlConnection(connStr);
cn.Open();
cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spGetCityData";
da = new SqlDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
cn.Close();
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in Gridview1.Rows)
{
var check = row.FindControl("Chk") as CheckBox;
if (check.Checked)
{
var id = row.FindControl("lblId") as Label;
cn = new SqlConnection(connStr);
cn.Open();
cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spDeleteCityRecord";
cmd.Parameters.AddWithValue("@id", SqlDbType.Int).Value=id.Text;
da = new SqlDataAdapter(cmd);
da.Fill(dt);
int rowAffected=cmd.ExecuteNonQuery();
if (rowAffected >= 0)
{
Response.Write("Deleted Successfully");
}
else
{
Response.Write("Deleted Unsuccessfully");
}
GetDatatoGv();
cn.Close();
}
}
}
}
web.config:-
<connectionStrings>
<add name="conStr" connectionString="server=localhost;database=subsdb;user id=sa;pwd=123"/>
</connectionStrings>
For create Table and SP:-
use subsdb
create table city(cid int constraints p_key primary key,cname varchar(30))
create proc spDeleteCityRecord(@id int)
as
begin
delete from city where cid=@id;
end
create proc spGetCityData
as
begin
select cname,cid from city
end
Default.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ChkBx wth GrdVw Operations</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<asp:gridview ID="Gridview1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="cid">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Bind("cid") %>'></asp:Label>
</ItemTemplate>
<%--<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("cid") %>'></asp:TextBox>
</EditItemTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="cname">
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%# Bind("cname") %>'></asp:Label>
</ItemTemplate>
<%-- <EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("cname") %>'></asp:TextBox>
</EditItemTemplate>--%>
</asp:TemplateField>
</Columns>
</asp:gridview>
<br />
<asp:Button ID="btnDelete" runat="server" Text="Delete"
onclick="btnDelete_Click" />
</center>
</div>
</form>
</body>
</html>
Default.aspx.cs:-
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
static SqlConnection cn;
static SqlCommand cmd;
static SqlDataAdapter da;
static DataTable dt;
static string connStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetDatatoGv();
}
}
void GetDatatoGv()
{
cn = new SqlConnection(connStr);
cn.Open();
cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spGetCityData";
da = new SqlDataAdapter(cmd);
dt = new DataTable();
da.Fill(dt);
cn.Close();
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in Gridview1.Rows)
{
var check = row.FindControl("Chk") as CheckBox;
if (check.Checked)
{
var id = row.FindControl("lblId") as Label;
cn = new SqlConnection(connStr);
cn.Open();
cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spDeleteCityRecord";
cmd.Parameters.AddWithValue("@id", SqlDbType.Int).Value=id.Text;
da = new SqlDataAdapter(cmd);
da.Fill(dt);
int rowAffected=cmd.ExecuteNonQuery();
if (rowAffected >= 0)
{
Response.Write("Deleted Successfully");
}
else
{
Response.Write("Deleted Unsuccessfully");
}
GetDatatoGv();
cn.Close();
}
}
}
}
web.config:-
<connectionStrings>
<add name="conStr" connectionString="server=localhost;database=subsdb;user id=sa;pwd=123"/>
</connectionStrings>
For create Table and SP:-
use subsdb
create table city(cid int constraints p_key primary key,cname varchar(30))
create proc spDeleteCityRecord(@id int)
as
begin
delete from city where cid=@id;
end
create proc spGetCityData
as
begin
select cname,cid from city
end
No comments:
Post a Comment