In this article we will learn how to use grid view rowcommand event to insert, update and delete the record in asp.net with c#
now for that i have created table named as usertbl. and took three field first one is id , name and company.
same way three text box .to insert the value into table.
4 rajana nit
38 dev lolo
45 dev lolol
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
protected void Page_Init(object sender, EventArgs e)
{
}
protected void Page_Load(object sender, EventArgs e)
{
display();
}
protected void btnsbmit_Click(object sender, EventArgs e) //for insertion
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("insert_db", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@nm", txtnm.Text);
cmd.Parameters.AddWithValue("@comp", txtcom .Text );
cmd.ExecuteNonQuery();
cmd.Dispose();
}
catch { }
finally
{
con.Close();
}
display();
}
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "select")
{
ViewState["val"] = Convert.ToInt32(e.CommandArgument);
GridViewRow row = (GridViewRow)(((LinkButton )e.CommandSource).NamingContainer);
//taking gridview index so we can that row values.
int selIndex = row.RowIndex;
string last = grd.Rows[selIndex].Cells[2].Text;
string comp = grd.Rows[selIndex].Cells[3].Text;
txtnm.Text = last;
txtcom.Text = comp;
}
if (e.CommandName =="del")
{
int del = Convert.ToInt32(e.CommandArgument);
ViewState["val"] = del;
SqlCommand cmd = new SqlCommand("delete from usertbl where id='" + del + "'", con);
try
{
con.Open();
cmd.ExecuteNonQuery();
lblmsg.Text = "Record deleted succesfully";
}
catch { }
finally
{
con.Close();
}
display();
}
}
protected void btnupdate_Click(object sender, EventArgs e)
{
int i = 0;
int last =Convert.ToInt32 ( ViewState["val"]);
//string l1 = Replace(txtnm.Text, "", "-");
string str = "update usertbl set name='" + txtnm .Text + "', company ='" + txtcom .Text +"' where id='" + last + "'";
con.Open();
SqlCommand cndupdate = new SqlCommand(str, con);
i=cndupdate.ExecuteNonQuery();
if (i >= 1)
{
lblmsg.Text ="Record updated succesfully";
}
con.Close();
display();
}
public void display()
{
DataSet ds = new DataSet();
con.Open();
SqlCommand cmd1 = new SqlCommand("select * from usertbl", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd1);
sda.Fill(ds);
ViewState["ds"] = ds;
grd.DataSource = ds;
grd.DataBind();
con.Close();
}
protected void grd_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void grd_Sorting(object sender, GridViewSortEventArgs e)
{
DataSet ds = new DataSet ();
ds=(DataSet )(ViewState["ds"]);
// DataView dv = new DataView(ViewState ["ds"]);
}
}
<%@ 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">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td>
<asp:Label runat="server" ID="lbl">name</asp:Label></td>
<td><asp:TextBox runat="server" ID="txtnm"></asp:TextBox></td>
</tr>
<tr><td>
<asp:Label runat="server" ID="Label1">Company</asp:Label></td>
<td><asp:TextBox runat="server" ID="txtcom"></asp:TextBox></td>
</tr>
<tr><td><asp:Button runat="server" ID="btnsbmit" onclick="btnsbmit_Click"
Text="Submit" /></td> <td>
<asp:Button runat="server" ID="btnupdate"
Text="update" onclick="btnupdate_Click" Visible="False" /><td></tr>
<tr><td><asp:Label runat ="server" ID="lblmsg"></asp:Label></td></tr>
</table>
<asp:GridView ID="grd" runat="server" onrowcommand="grd_RowCommand"
onsorting="grd_Sorting">
<Columns >
<asp:TemplateField HeaderText ="Edit" >
<ItemTemplate >
<asp:LinkButton ID="edit" CommandName ="select" runat ="server" Text ="Edit" CommandArgument ='<%# DataBinder.Eval(Container, "DataItem.id") %>' ></asp:LinkButton>
<asp:LinkButton ID="delete" CommandName ="del" runat ="server" Text ="delete" CommandArgument ='<%#DataBinder.Eval(Container,"DataItem.id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
now for that i have created table named as usertbl. and took three field first one is id , name and company.
same way three text box .to insert the value into table.
4 rajana nit
38 dev lolo
45 dev lolol
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
protected void Page_Init(object sender, EventArgs e)
{
}
protected void Page_Load(object sender, EventArgs e)
{
display();
}
protected void btnsbmit_Click(object sender, EventArgs e) //for insertion
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("insert_db", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@nm", txtnm.Text);
cmd.Parameters.AddWithValue("@comp", txtcom .Text );
cmd.ExecuteNonQuery();
cmd.Dispose();
}
catch { }
finally
{
con.Close();
}
display();
}
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "select")
{
ViewState["val"] = Convert.ToInt32(e.CommandArgument);
GridViewRow row = (GridViewRow)(((LinkButton )e.CommandSource).NamingContainer);
//taking gridview index so we can that row values.
int selIndex = row.RowIndex;
string last = grd.Rows[selIndex].Cells[2].Text;
string comp = grd.Rows[selIndex].Cells[3].Text;
txtnm.Text = last;
txtcom.Text = comp;
}
if (e.CommandName =="del")
{
int del = Convert.ToInt32(e.CommandArgument);
ViewState["val"] = del;
SqlCommand cmd = new SqlCommand("delete from usertbl where id='" + del + "'", con);
try
{
con.Open();
cmd.ExecuteNonQuery();
lblmsg.Text = "Record deleted succesfully";
}
catch { }
finally
{
con.Close();
}
display();
}
}
protected void btnupdate_Click(object sender, EventArgs e)
{
int i = 0;
int last =Convert.ToInt32 ( ViewState["val"]);
//string l1 = Replace(txtnm.Text, "", "-");
string str = "update usertbl set name='" + txtnm .Text + "', company ='" + txtcom .Text +"' where id='" + last + "'";
con.Open();
SqlCommand cndupdate = new SqlCommand(str, con);
i=cndupdate.ExecuteNonQuery();
if (i >= 1)
{
lblmsg.Text ="Record updated succesfully";
}
con.Close();
display();
}
public void display()
{
DataSet ds = new DataSet();
con.Open();
SqlCommand cmd1 = new SqlCommand("select * from usertbl", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd1);
sda.Fill(ds);
ViewState["ds"] = ds;
grd.DataSource = ds;
grd.DataBind();
con.Close();
}
protected void grd_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void grd_Sorting(object sender, GridViewSortEventArgs e)
{
DataSet ds = new DataSet ();
ds=(DataSet )(ViewState["ds"]);
// DataView dv = new DataView(ViewState ["ds"]);
}
}
<%@ 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">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td>
<asp:Label runat="server" ID="lbl">name</asp:Label></td>
<td><asp:TextBox runat="server" ID="txtnm"></asp:TextBox></td>
</tr>
<tr><td>
<asp:Label runat="server" ID="Label1">Company</asp:Label></td>
<td><asp:TextBox runat="server" ID="txtcom"></asp:TextBox></td>
</tr>
<tr><td><asp:Button runat="server" ID="btnsbmit" onclick="btnsbmit_Click"
Text="Submit" /></td> <td>
<asp:Button runat="server" ID="btnupdate"
Text="update" onclick="btnupdate_Click" Visible="False" /><td></tr>
<tr><td><asp:Label runat ="server" ID="lblmsg"></asp:Label></td></tr>
</table>
<asp:GridView ID="grd" runat="server" onrowcommand="grd_RowCommand"
onsorting="grd_Sorting">
<Columns >
<asp:TemplateField HeaderText ="Edit" >
<ItemTemplate >
<asp:LinkButton ID="edit" CommandName ="select" runat ="server" Text ="Edit" CommandArgument ='<%# DataBinder.Eval(Container, "DataItem.id") %>' ></asp:LinkButton>
<asp:LinkButton ID="delete" CommandName ="del" runat ="server" Text ="delete" CommandArgument ='<%#DataBinder.Eval(Container,"DataItem.id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
0 comments: