You are on page 1of 5

Programm for edit,delete,update in table using grid view

using using using using using using using using using using using using using System; System.Collections.Generic; System.Linq; System.Web; System.Web.UI; System.Web.UI.WebControls; System.Data; System.Collections; System.Security; System.Data.SqlClient; System.Web.UI.WebControls.WebParts; System.Web.UI.HtmlControls; System.Xml.Linq;

public partial class _Default : System.Web.UI.Page { DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(); SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=rahul;Integrated Security=True");

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindData(); } } public void BindData() { SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=rahul;Integrated Security=True"); con.Open(); SqlCommand cmd = new SqlCommand("Select * from employee where Deleted='N'", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); Grid.DataSource = ds; Grid.DataBind(); con.Close(); } protected void GridView1_PageIndexChanged(object sender, DataGridPageChangedEventArgs e) { Grid.PageIndex = e.NewPageIndex; BindData(); }

protected void Button1_Click(object sender, EventArgs e) { SqlConnection con; con = new SqlConnection("Data Source=.;Initial Catalog=rahul;Integrated Security=True"); con.Open(); SqlCommand cmd; cmd = new SqlCommand("Insert into employee ([Country Name],[City Name], [Name],[Salary],[Department]) Values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')", con); cmd.ExecuteNonQuery(); con.Close(); BindData(); } protected void Grid_RowEditing(object sender, GridViewEditEventArgs e) { Grid.EditIndex = e.NewEditIndex; BindData(); } protected void Grid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { Grid.EditIndex = -1; BindData(); } protected void Grid_RowDeleting(object sender, GridViewDeleteEventArgs e) { GridViewRow row = (GridViewRow)Grid.Rows[e.RowIndex]; con.Open(); string query = "update employee set Deleted='Y' where Name='" + row.Cells[2].Text.ToString() + "'"; SqlCommand cmd = new SqlCommand(query, con); cmd.ExecuteNonQuery(); con.Close(); BindData(); } protected void Grid_RowUpdating(object sender, GridViewUpdateEventArgs e) { string strCname = (Grid.Rows[e.RowIndex].Cells[0].Controls[0] as TextBox).Text; string strcity = (Grid.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text; string name = (Grid.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text; string sal = (Grid.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text; string dep = (Grid.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text; string query = " update employee set [Country Name]='" + strCname + "',[City Name]='" + strcity + "',Salary=" + sal + ",Department='" + dep + "'where Name='" + name + "'"; Grid.EditIndex = -1; con.Open(); SqlCommand cmd = new SqlCommand(query, con); cmd.ExecuteNonQuery(); con.Close(); BindData(); }

Codes of HTML:-

<%@ 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></title> </head> <body> <form id="form1" runat="server"> <div> <table class="style20"> <tr> <td class="style3"> <asp:Label ID="Label1" runat="server" CssClass="style15" Text="Country Name:-"></asp:Label> </td> <td align="center" class="style2" valign="middle"> <asp:TextBox ID="TextBox1" runat="server" CssClass="style8" Width="134px"></asp:TextBox> </td> </tr> <tr> <td class="style3"> <asp:Label ID="Label3" runat="server" CssClass="style14" Text=" Name:-"></asp:Label> </td> <td align="center" class="style2" valign="middle"> <asp:TextBox ID="TextBox2" runat="server" CssClass="style9" Width="135px"></asp:TextBox> </td> </tr> <tr> <td class="style3"> <asp:Label ID="Label2" runat="server" CssClass="style17" Text="City Name:-"></asp:Label> </td> <td align="center" class="style2" valign="middle"> <asp:TextBox ID="TextBox3" runat="server" CssClass="style10" Width="135px"></asp:TextBox> </td> </tr> <tr> <td class="style3"> <asp:Label ID="Label4" runat="server" CssClass="style18" Text="Salary:-"></asp:Label> </td> <td align="center" class="style2" valign="middle"> <asp:TextBox ID="TextBox4" runat="server" CssClass="style11" Width="136px"></asp:TextBox> </td>

</tr> <tr> <td class="style3"> <asp:Label ID="Label5" runat="server" CssClass="style16" Text="Department:-"></asp:Label> </td> <td align="center" class="style2" valign="middle"> <asp:TextBox ID="TextBox5" runat="server" Width="137px" ></asp:TextBox> </td> </tr> <tr> <td class="style3"> &nbsp;</td> <td align="center" class="style2" valign="middle"> &nbsp;</td> </tr> <tr> <td class="style3"> &nbsp;</td> <td align="center" class="style2" valign="middle"> <asp:Button ID="Button1" runat="server" CssClass="style19" Text="Save" Width="103px" onclick="Button1_Click" /> </td> </tr> </table> </div> <asp:GridView ID="Grid" runat="server" CssClass="style4" Height="91px" AutoGenerateColumns="False" EnablePersistedSelection="True" EnableSortingAndPagingCallbacks="True" ShowFooter="True" ShowHeaderWhenEmpty="True" Width="681px" DataKeyNames="name" onrowdeleting="Grid_RowDeleting" onrowediting="Grid_RowEditing" onrowcancelingedit="Grid_RowCancelingEdit" onrowupdating="Grid_RowUpdating"> <Columns> <asp:BoundField HeaderText="Country Name" DataField="Country Name" /> <asp:BoundField HeaderText="City Name" DataField="City Name" /> <asp:BoundField HeaderText="Name" DataField="Name" /> <asp:BoundField HeaderText="Salary" DataField="Salary" /> <asp:BoundField HeaderText="Department" DataField="Department" /> <asp:CommandField ShowEditButton="True" /> <asp:CommandField ShowDeleteButton="True" /> </Columns> </asp:GridView> </form> </body> </html>

You might also like