Sie sind auf Seite 1von 3

using System; using System.Data; using System.Configuration; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.

Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack == false) { gridbind(); } } private void gridbind() { SqlDataAdapter adp = new SqlDataAdapter("select * from r1", Configuratio nManager.ConnectionStrings["AConnectionString"].ConnectionString); DataSet ds = new DataSet(); adp.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; gridbind(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { Int32 pid; string pname, padd; pid = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lb 1")).Text);//fetch data key /primary for pname = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("t1")).Text; padd = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("t2")).Text; SqlConnection con = new SqlConnection();//Creation of new sqlconn instan ce of the name con. con.ConnectionString = ConfigurationManager.ConnectionStrings["AConnecti onString"].ConnectionString; con.Open(); SqlCommand cmd = new SqlCommand();//sql command cmd.CommandText = "update r1 set pname=@pname,padd=@padd where pid=@pid" ; cmd.Connection = con;//queries were get exceuted in con database. cmd.Parameters.Add("@pid", SqlDbType.Int).Value = pid;//creation of para meters + sqlDbtype.int(enumeration data cmd.Parameters.Add("@pname", SqlDbType.VarChar, 50).Value = pname; cmd.Parameters.Add("@padd", SqlDbType.VarChar, 50).Value = padd; cmd.ExecuteNonQuery();//just 2 execute the query and return the number o f record affected cmd.Dispose();//to free the command resources GridView1.EditIndex = -1;

gridbind(); } protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e) { } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditE ventArgs e) { GridView1.EditIndex = -1; gridbind(); } }

asxp

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherit s="Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or g/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCanceli ngEdit" OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText="pid"> <ItemTemplate> <%#Eval("pid") %> </ItemTemplate> <EditItemTemplate> <asp:Label ID="lb1" Text='<%#Eval("pid") %>' runat="server"> </asp:Label> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="employee detail">

<ItemTemplate> <b>pname:</b><%#Eval("pname") %><br></br> <b>padd:</b><%#Eval("padd") %><br></br> <asp:LinkButton ID="lk1" Text="edit" CommandName="edit" runat="server"> </asp:LinkButton> </ItemTemplate> <EditItemTemplate> <b>name:</b><asp:TextBox ID="t1" Text='<%#Eval("pname") %>' runat="server">< /asp:TextBox> <b>add:</b><asp:TextBox ID="t2" Text='<%#Eval("padd") %>' runat="server"></a sp:TextBox> <asp:LinkButton ID="lk2" Text="update" CommandName="update" runat="server">< /asp:LinkButton> <asp:LinkButton ID="lk3" Text="cancel" CommandName="cancel" runat="server">< /asp:LinkButton> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>

Das könnte Ihnen auch gefallen