- Back to Home »
- ADO.NET , ASP.NET »
- GridView Edit Update Delete
Posted by :
Sudhir Chekuri
Wednesday, 22 February 2017
SQL Queries
create table Tbl_Admin(Aid int identity primary key, Username varchar(50),Pwd varchar(50),PhoneNumber varchar(20),createdtime datetime)
insert into Tbl_Admin values('sudhir','sud',999898898,getdate())
insert into Tbl_Admin values('raj','raj123',999898897,getdate())
insert into Tbl_Admin values('gopal','gopal123',999898896,getdate())
insert into Tbl_Admin values('sai','sai123',999898895,getdate())
select * from Tbl_Admin
ASP.NET Source Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %>
<!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" AutoGenerateColumns="False" DataKeyNames="Aid"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="Aid" HeaderText="Aid" />
<asp:BoundField DataField="Username" HeaderText="Username" />
<asp:BoundField DataField="Pwd" HeaderText="Password" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
ASP.NET Source Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %>
<!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" AutoGenerateColumns="False" DataKeyNames="Aid"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="Aid" HeaderText="Aid" />
<asp:BoundField DataField="Username" HeaderText="Username" />
<asp:BoundField DataField="Pwd" HeaderText="Password" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
C#.NET Code
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;
namespace WebApplication3
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//only for first time
GridFill();
}
}
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=JobPortal;Integrated Security=True");
public void GridFill()
{
//GridView Fill
SqlCommand cmd = new SqlCommand("select * from Tbl_Admin", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
// Label lbldeleteid = (Label)row.FindControl("lblID");
con.Open();
SqlCommand cmd = new SqlCommand("delete FROM tbl_admin where aid='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
cmd.ExecuteNonQuery();
con.Close();
GridFill();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridFill();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int aid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
// Label lblID = (Label)row.FindControl("lblID");
//TextBox txtname=(TextBox)gr.cell[].control[];
TextBox textName = (TextBox)row.Cells[2].Controls[0];
TextBox textpwd = (TextBox)row.Cells[3].Controls[0];
//to convert textboxes to labels in gridview
GridView1.EditIndex = -1;
con.Open();
SqlCommand cmd = new SqlCommand("update tbl_admin set username='" + textName.Text + "',pwd='" + textpwd.Text + "' where aid='" + aid + "'", con);
cmd.ExecuteNonQuery();
con.Close();
GridFill();
}
}
}