001.
using
System;
002.
using
System.Collections.Generic;
003.
using
System.Linq;
004.
using
System.Web;
005.
using
System.Web.UI;
006.
using
System.Web.UI.WebControls;
007.
using
System.Data;
008.
using
System.Data.SqlClient;
009.
using
System.Web.Configuration;
010.
011.
namespace
EditUpdateDelete
012.
{
013.
public
partial
class
WebForm1 : System.Web.UI.Page
014.
{
015.
protected
void
Page_Load(
object
sender, EventArgs e)
016.
{
017.
if
(!IsPostBack)
018.
{
019.
ShowData();
020.
}
021.
}
022.
023.
string
strCon = WebConfigurationManager.ConnectionStrings[
"AdminRoleConnectionString"
].ConnectionString;
024.
025.
protected
void
ShowData()
026.
{
027.
SqlConnection con =
new
SqlConnection(strCon);
028.
con.Open();
029.
030.
string
sql =
"select ID,fname,lname from AllUser"
;
031.
SqlDataAdapter cmd =
new
SqlDataAdapter(sql, con);
032.
DataSet ds =
new
DataSet();
033.
cmd.Fill(ds);
034.
035.
GridView1.DataSource = ds;
036.
GridView1.DataBind();
037.
038.
if
(ds.Tables[0].Rows.Count > 0)
039.
{
040.
GridView1.DataSource = ds;
041.
GridView1.DataBind();
042.
}
043.
con.Close();
044.
}
045.
046.
protected
void
GridView1_RowEditing(
object
sender, GridViewEditEventArgs e)
047.
{
048.
GridView1.EditIndex = e.NewEditIndex;
049.
ShowData();
050.
}
051.
052.
protected
void
GridView1_RowCancelingEdit(
object
sender, GridViewCancelEditEventArgs e)
053.
{
054.
GridView1.EditIndex = -1;
055.
ShowData();
056.
}
057.
058.
protected
void
GridView1_RowUpdating(
object
sender, GridViewUpdateEventArgs e)
059.
{
060.
Label ID = GridView1.Rows[e.RowIndex].FindControl(
"lb_ID"
)
as
Label;
061.
TextBox Firstname = GridView1.Rows[e.RowIndex].FindControl(
"txt1"
)
as
TextBox;
062.
TextBox Lastname = GridView1.Rows[e.RowIndex].FindControl(
"txt2"
)
as
TextBox;
063.
064.
SqlConnection con =
new
SqlConnection(strCon);
065.
con.Open();
066.
067.
string
sql2 =
"Update AllUser set fname='"
+ Firstname.Text +
"', lname = '"
+ Lastname.Text +
"' where ID =+ Convert.ToInt32(ID.Text)+"
;
068.
SqlCommand cmd =
new
SqlCommand(sql2, con);
069.
cmd.ExecuteNonQuery();
070.
con.Close();
071.
072.
GridView1.EditIndex = -1;
073.
ShowData();
074.
}
075.
076.
protected
void
DeleteRecord(
int
IDUser)
077.
{
078.
SqlConnection con =
new
SqlConnection(strCon);
079.
SqlCommand cmd =
new
SqlCommand(
"delete from AllUser where ID=@ID"
, con);
080.
cmd.Parameters.AddWithValue(
"@ID"
, IDUser);
081.
con.Open();
082.
cmd.ExecuteNonQuery();
083.
con.Close();
084.
}
085.
086.
protected
void
Button1_Click(
object
sender, EventArgs e)
087.
{
088.
foreach
(GridViewRow grow
in
GridView1.Rows)
089.
{
090.
091.
CheckBox chkdel = (CheckBox)grow.FindControl(
"chkdel"
);
092.
093.
if
(chkdel.Checked)
094.
{
095.
int
IDUser = Convert.ToInt32(grow.Cells[1].Text);
096.
DeleteRecord(IDUser);
097.
}
098.
}
099.
}
100.
}
101.
}