Gridview Edit Update Delete

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

Default.aspx.

cs
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; public partial class _Default : System.Web.UI.Page { SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=krishna;User ID=sa"); SqlCommand com = new SqlCommand(); protected void Page_Load(object sender, EventArgs e) { con.Open(); } protected void Button1_Click(object sender, EventArgs e) { if (txt1.Text != "") { com = new SqlCommand("insert into amain values(@eid,@ename,@edesg,@salary,@eaddress)", con); com.Parameters.AddWithValue("@eid", Convert.ToInt16(txt1.Text)); com.Parameters.AddWithValue("@ename", txt2.Text); com.Parameters.AddWithValue("@edesg", txt3.Text); com.Parameters.AddWithValue("@salary", Convert.ToInt16(txt4.Text)); com.Parameters.AddWithValue("@eaddress", txt5.Text); com.ExecuteNonQuery(); Response.Redirect("Default2.aspx"); } else { Response.Redirect("Default2.aspx"); } } }

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 runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td> <asp:Label ID="lbl1" runat="server" Text="ID"></asp:Label> </td> <td> <asp:TextBox ID="txt1" runat="server"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label ID="lbl2" runat="server" Text="Name"></asp:Label> </td> <td> <asp:TextBox ID="txt2" runat="server"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label ID="lbl3" runat="server" Text="Designation"></asp:Label> </td> <td> <asp:TextBox ID="txt3" runat="server"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label ID="lbl4" runat="server" Text="salary"></asp:Label> </td> <td> <asp:TextBox ID="txt4" runat="server"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label ID="lbl5" runat="server" Text="Address"></asp:Label> </td> <td> <asp:TextBox ID="txt5" runat="server"></asp:TextBox> </td> </tr> <tr> <td > <asp:Button ID="Button1" runat="server" Text="Insert" onclick="Button1_Click" />

</td> </tr> </table> </div> </form> </body> </html>

Default2.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!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 runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" onrowcancelingedit="GridView1_RowCancelingEdit" onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"> <Columns> <asp:CommandField ShowEditButton="True" /> <asp:CommandField ShowDeleteButton="True" /> <asp:HyperLinkField DataNavigateUrlFields="eid" DataNavigateUrlFormatString="Default3.aspx?eid={0}" HeaderText="eid" Text="eid" /> <asp:HyperLinkField HeaderText="Back" NavigateUrl="~/Default.aspx" Text="Back" /> </Columns> </asp:GridView> </div> </form> </body> </html>

Default2.aspx.cs
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; public partial class Default2 : System.Web.UI.Page { SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=krishna;User ID=sa"); SqlCommand com = new SqlCommand(); SqlDataAdapter da=new SqlDataAdapter(); DataSet ds=new DataSet(); protected void Page_Load(object sender, EventArgs e) { con.Open(); if (!IsPostBack) { bind(); } } public void bind() { com = new SqlCommand("select * from amain", con); da = new SqlDataAdapter(com); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { com = new SqlCommand("update amain set ename=@ename,edesg=@edesg where eid=@eid", con); com.Parameters.AddWithValue("@ename", ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text); com.Parameters.AddWithValue("@edesg", ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text); com.Parameters.AddWithValue("@eid", ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text); com.ExecuteNonQuery(); GridView1.EditIndex = -1; bind(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; bind(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) {

com = new SqlCommand("delete from amain where eid=@eid", con); com.Parameters.AddWithValue("@eid", GridView1.Rows[e.RowIndex].Cells[2].Text); com.ExecuteNonQuery(); GridView1.EditIndex = -1; bind(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; bind(); } }

Default3.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> <!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 runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> </div> </form> </body> </html>

Default3.aspx.cs:
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; public partial class Default3 : System.Web.UI.Page

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=krishna;User ID=sa"); SqlCommand com = new SqlCommand(); SqlDataAdapter da = new SqlDataAdapter(); DataSet ds = new DataSet(); protected void Page_Load(object sender, EventArgs e) { con.Open(); if (!IsPostBack) { bind(); } } public void bind() { com = new SqlCommand("select * from amain where eid='"+Request.QueryString["eid"]+"'", con); da = new SqlDataAdapter(com); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy