Code of Delete:-
1. Make the Procedure
ALTER procedure [dbo].[delete1]
@id int
as
begin
delete from registration where id=@id;
end
2.Write the code on rowdeleting event:-
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e){
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
SqlCommand cmd = new SqlCommand("delete1", con);
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
string id = ((Label)GridView1.Rows[e.RowIndex].Cells[2].Controls[1]).Text;
cmd.Parameters.Add("@id", SqlDbType.Int).Value =int.Parse(id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
Code of Updating in Database:-
1. Make the store procedure for update:-
ALTER procedure [dbo].[update1]
@id int,
@name nvarchar(34),
@email nvarchar(34),
@password nvarchar(34),
@address nvarchar(23)
as
begin
update registration set name=@name,email=@email,password=@password,address=@address where id=@id;
end
2.Write the code on rowupdating event:-
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
SqlCommand cmd = new SqlCommand("update1", con);
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
string id = (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("Tid"))).Text);
string name = (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("Tname"))).Text);
string email = (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("Temail"))).Text);
string password = (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("Tpassword"))).Text);
string address = (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("Taddress"))).Text);
cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
cmd.Parameters.Add("@name", SqlDbType.NVarChar, 30).Value = name;
cmd.Parameters.Add("@email", SqlDbType.NVarChar, 30).Value = email;
cmd.Parameters.Add("@password", SqlDbType.NVarChar, 30).Value = password;
cmd.Parameters.Add("@address", SqlDbType.NVarChar, 30).Value = address;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
Code of Editing in Gridview:-
Write the code on rowediting event:-
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
grdbind();
}
Code of Cancelling in Gridview:-
Write this on rowcancelEdit event:-
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
grdbind();
}
Make the function on the name of grdbind():-
public void grdbind()
{
SqlDataAdapter sda = new SqlDataAdapter("select id,name,email,password,address from registration", System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
If you want to show all record from database on button than please write this code on button:-
SqlDataAdapter sda = new SqlDataAdapter("select * from registration",System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
No comments:
Post a Comment