Using Gridview Rowcommand Edit, Delete and Update..........
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function ConfirmOnDelete()
{
if (confirm("Are you sure to delete?")==true)
return true;
else
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width:100%; height:383px;" cellspacing="0" cellpadding="0">
<tr>
<td style="width:200px">Emp Id</td>
<asp:TextBox ID="TextBox1" runat="server" Height="21px" Width="171px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width:200px"></td>
<td style="width:200px">Emp Name</td>
<td style="width:200px">
<asp:TextBox ID="TextBox2" runat="server" Height="21px" Width="171px"></asp:TextBox>
</td>
<tr>
<td style="width:200px">Emp Phone</td>
<td style="width:200px">
<asp:TextBox ID="TextBox3" runat="server" Height="21px" Width="171px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width:200px">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Insert"
ToolTip="Insert" />
<asp:Button ID="btn_cancle" runat="server" onclick="btn_cancle_Click"
Text="Cancle" ToolTip="Cancle" Visible="False" />
<asp:Button ID="btn_clear" runat="server" onclick="btn_clear_Click"
Text="Clear" />
</td>
<td style="width:200px">
<asp:Button ID="Searchbtn" runat="server" onclick="Searchbtn_Click"
Text="Search" />
</td>
</tr>
<tr>
<td style="width:200px">
<asp:Button ID="Button2" runat="server" Text="Button" />
</td>
<td colspan="2" style="width:200px">
<asp:GridView ID="GridView1" runat="server" Width="366px" OnRowCommand="GridView1_RowCommand"
AutoGenerateColumns="False" DataKeyNames="id" Height="186px"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing">
<Columns>
<asp:TemplateField HeaderText="Emp Id">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%#bind("id") %>'>'</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emp Name">
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%#bind("name") %>'>'</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Phone">
<ItemTemplate>
<asp:Label ID="lblphone" runat="server" Text='<%#bind("phone") %>'>'</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button ID="editbtn" runat="server" CommandName="edit" CausesValidation="false" Text="Edit" ToolTip="Edit" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="deletebtn" runat="server" CommandName="delete" CausesValidation="false" Text="Delete" ToolTip="Delete" OnClientClick="ConfirmOnDelete();" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
<td style="width:200px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
aspx.cs code
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Configuration;
using System.IO;
public partial class sk : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
SqlCommand cmd;
SqlDataAdapter da;
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
show();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Button1.ToolTip == "Insert")
{
con.Open();
string str = "insert into text(id,name,phone) values(@id,@name,@phone)";
cmd = new SqlCommand(str, con);
cmd.Parameters.AddWithValue("@id", TextBox1.Text);
cmd.Parameters.AddWithValue("@name", TextBox2.Text);
cmd.Parameters.AddWithValue("@phone", TextBox3.Text);
cmd.ExecuteNonQuery();
Response.Write("<script>alert('Record saved!')</script>");
con.Close();
show();
}
else if (Button1.ToolTip == "Update")
{
int id = Convert.ToInt32(ViewState["pid"]);
update(id);
}
}
public void show()
{
con.Open();
da = new SqlDataAdapter("select *from text",con);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
//GridView2.DataSource = ds;
//GridView2.DataBind();
}
protected void Btnedit_Click(object sender, EventArgs e)
{
//string id = GridView1.SelectedDataKey.ToString();
string a = GridView1.SelectedIndex.ToString();
con.Open();
string str = "select *from text where id='" + a + "'";
cmd = new SqlCommand(str, con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
TextBox1.Text = dr[0].ToString();
TextBox2.Text = dr[1].ToString();
TextBox3.Text = dr[2].ToString();
}
}
protected void Searchbtn_Click(object sender, EventArgs e)
{
con.Open();
string str = "select *from text where id='"+TextBox1.Text+"'";
cmd = new SqlCommand(str, con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
//TextBox1.Text = dr[0].ToString();
TextBox2.Text = dr[1].ToString();
TextBox3.Text = dr[2].ToString();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "edit")
{
Button btn = (Button)e.CommandSource;
GridViewRow grdrow = ((GridViewRow)btn.NamingContainer);
//Console.Write("<script>alert(empid)</script>");
//ViewState["id"] = e.CommandArgument;
Label id = (Label)grdrow.FindControl("lblid");
Label name = (Label)grdrow.FindControl("lblname");
Label phone = (Label)grdrow.FindControl("lblphone");
TextBox1.Text = id.Text;
TextBox2.Text = name.Text;
TextBox3.Text = phone.Text;
Button1.ToolTip = "Update";
Button1.Text = "Update";
btn_cancle.Visible = true;
int empid = Convert.ToInt32(GridView1.DataKeys[grdrow.RowIndex].Values["id"].ToString());
ViewState["pid"] = empid.ToString();
//update(empid)
}
if (e.CommandName == "delete")
{
Button btn = (Button)e.CommandSource;
GridViewRow grdrow = ((GridViewRow)btn.NamingContainer);
int empid = Convert.ToInt32(GridView1.DataKeys[grdrow.RowIndex].Values["id"].ToString());
//string str = "delete from text where id=" + empid + "";
string str = "update text set isactive=0 where id=" + empid + "";
cmd = new SqlCommand(str,con);
con.Open();
cmd.ExecuteNonQuery();
Response.Write("<script>alert('Record deleted secussfully...')</script>");
con.Close();
show();
}
}
public void update(int id)
{
int pid = id;
string str = "update text set name=@name,phone=@phone where id="+pid+"";
cmd = new SqlCommand(str, con);
cmd.Parameters.AddWithValue("@name", TextBox2.Text);
cmd.Parameters.AddWithValue("@phone", TextBox3.Text);
con.Open();
cmd.ExecuteNonQuery();
Console.Write("<script>alert('Record saved!')</script>");
con.Close();
show();
}
protected void btn_cancle_Click(object sender, EventArgs e)
{
Button1.Text = "Insert";
btn_cancle.Visible = false;
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
}
protected void btn_clear_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function ConfirmOnDelete()
{
if (confirm("Are you sure to delete?")==true)
return true;
else
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width:100%; height:383px;" cellspacing="0" cellpadding="0">
<tr>
<td style="width:200px">Emp Id</td>
<asp:TextBox ID="TextBox1" runat="server" Height="21px" Width="171px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width:200px"></td>
<td style="width:200px">Emp Name</td>
<td style="width:200px">
<asp:TextBox ID="TextBox2" runat="server" Height="21px" Width="171px"></asp:TextBox>
</td>
<tr>
<td style="width:200px">Emp Phone</td>
<td style="width:200px">
<asp:TextBox ID="TextBox3" runat="server" Height="21px" Width="171px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width:200px">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Insert"
ToolTip="Insert" />
<asp:Button ID="btn_cancle" runat="server" onclick="btn_cancle_Click"
Text="Cancle" ToolTip="Cancle" Visible="False" />
<asp:Button ID="btn_clear" runat="server" onclick="btn_clear_Click"
Text="Clear" />
</td>
<td style="width:200px">
<asp:Button ID="Searchbtn" runat="server" onclick="Searchbtn_Click"
Text="Search" />
</td>
</tr>
<tr>
<td style="width:200px">
<asp:Button ID="Button2" runat="server" Text="Button" />
</td>
<td colspan="2" style="width:200px">
<asp:GridView ID="GridView1" runat="server" Width="366px" OnRowCommand="GridView1_RowCommand"
AutoGenerateColumns="False" DataKeyNames="id" Height="186px"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing">
<Columns>
<asp:TemplateField HeaderText="Emp Id">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%#bind("id") %>'>'</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emp Name">
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%#bind("name") %>'>'</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Phone">
<ItemTemplate>
<asp:Label ID="lblphone" runat="server" Text='<%#bind("phone") %>'>'</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button ID="editbtn" runat="server" CommandName="edit" CausesValidation="false" Text="Edit" ToolTip="Edit" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="deletebtn" runat="server" CommandName="delete" CausesValidation="false" Text="Delete" ToolTip="Delete" OnClientClick="ConfirmOnDelete();" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
<td style="width:200px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
aspx.cs code
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Configuration;
using System.IO;
public partial class sk : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
SqlCommand cmd;
SqlDataAdapter da;
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
show();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Button1.ToolTip == "Insert")
{
con.Open();
string str = "insert into text(id,name,phone) values(@id,@name,@phone)";
cmd = new SqlCommand(str, con);
cmd.Parameters.AddWithValue("@id", TextBox1.Text);
cmd.Parameters.AddWithValue("@name", TextBox2.Text);
cmd.Parameters.AddWithValue("@phone", TextBox3.Text);
cmd.ExecuteNonQuery();
Response.Write("<script>alert('Record saved!')</script>");
con.Close();
show();
}
else if (Button1.ToolTip == "Update")
{
int id = Convert.ToInt32(ViewState["pid"]);
update(id);
}
}
public void show()
{
con.Open();
da = new SqlDataAdapter("select *from text",con);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
//GridView2.DataSource = ds;
//GridView2.DataBind();
}
protected void Btnedit_Click(object sender, EventArgs e)
{
//string id = GridView1.SelectedDataKey.ToString();
string a = GridView1.SelectedIndex.ToString();
con.Open();
string str = "select *from text where id='" + a + "'";
cmd = new SqlCommand(str, con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
TextBox1.Text = dr[0].ToString();
TextBox2.Text = dr[1].ToString();
TextBox3.Text = dr[2].ToString();
}
}
protected void Searchbtn_Click(object sender, EventArgs e)
{
con.Open();
string str = "select *from text where id='"+TextBox1.Text+"'";
cmd = new SqlCommand(str, con);
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
//TextBox1.Text = dr[0].ToString();
TextBox2.Text = dr[1].ToString();
TextBox3.Text = dr[2].ToString();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "edit")
{
Button btn = (Button)e.CommandSource;
GridViewRow grdrow = ((GridViewRow)btn.NamingContainer);
//Console.Write("<script>alert(empid)</script>");
//ViewState["id"] = e.CommandArgument;
Label id = (Label)grdrow.FindControl("lblid");
Label name = (Label)grdrow.FindControl("lblname");
Label phone = (Label)grdrow.FindControl("lblphone");
TextBox1.Text = id.Text;
TextBox2.Text = name.Text;
TextBox3.Text = phone.Text;
Button1.ToolTip = "Update";
Button1.Text = "Update";
btn_cancle.Visible = true;
int empid = Convert.ToInt32(GridView1.DataKeys[grdrow.RowIndex].Values["id"].ToString());
ViewState["pid"] = empid.ToString();
//update(empid)
}
if (e.CommandName == "delete")
{
Button btn = (Button)e.CommandSource;
GridViewRow grdrow = ((GridViewRow)btn.NamingContainer);
int empid = Convert.ToInt32(GridView1.DataKeys[grdrow.RowIndex].Values["id"].ToString());
//string str = "delete from text where id=" + empid + "";
string str = "update text set isactive=0 where id=" + empid + "";
cmd = new SqlCommand(str,con);
con.Open();
cmd.ExecuteNonQuery();
Response.Write("<script>alert('Record deleted secussfully...')</script>");
con.Close();
show();
}
}
public void update(int id)
{
int pid = id;
string str = "update text set name=@name,phone=@phone where id="+pid+"";
cmd = new SqlCommand(str, con);
cmd.Parameters.AddWithValue("@name", TextBox2.Text);
cmd.Parameters.AddWithValue("@phone", TextBox3.Text);
con.Open();
cmd.ExecuteNonQuery();
Console.Write("<script>alert('Record saved!')</script>");
con.Close();
show();
}
protected void btn_cancle_Click(object sender, EventArgs e)
{
Button1.Text = "Insert";
btn_cancle.Visible = false;
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
}
protected void btn_clear_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
}
}