How to Edit, Update, Delete Record in DetailsView in ASP.Net

Here, we will learn how to edit, update, delete record to DetailsView in ASP.Net C#.

How to Display record in DetailsView in ASP.Net ?

How to do paging in DetailsView control in ASP.Net ?

First Bind Data to DetailsView control from sql server..

For Editing set AutogenerateEditButton = True at property of DetailsView control.

How to Edit, Update, Detele in DetailsView in ASP.Net C#.
How to Edit, Update, Detele in DetailsView in ASP.Net C#.

Now, we have Edit button on DetailsView control in ASP.Net.

When we click Edit Button that time we need to change mode of DetailsView control for Editing Records.

The CurrentMode of DetailsView contorl is ReadOnly we change the mode to Edit for Editing Record..

How to Edit, Update, Detele in DetailsView in ASP.Net C#.
How to Edit, Update, Detele in DetailsView in ASP.Net C#.

write below code at ModeChanging Event of DetailsView control for go to Edit mode.

We handle Edit Click Event and Cancel Click Event here…

 protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
if (DetailsView1.CurrentMode == DetailsViewMode.ReadOnly)
{
DetailsView1.ChangeMode(DetailsViewMode.Edit);
BindDetailsView();
}
else if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{

DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
BindDetailsView();
}
}

How to Edit, Update, Detele in DetailsView in ASP.Net C#.
How to Edit, Update, Detele in DetailsView in ASP.Net C#.

Now click Edit button to enable Edit mode of Details View control….

How to Edit, Update, Detele in DetailsView in ASP.Net C#.
How to Edit, Update, Detele in DetailsView in ASP.Net C#.

If you click Cancel Button then DetailsView back to original  Readonly mode.

Here, you can change value of Name and City click update button to update Record to database.

For update we take here ItemUpdating Event…

How to Edit, Update, Detele in DetailsView in ASP.Net C#.
How to Edit, Update, Detele in DetailsView in ASP.Net C#.

write below code at ItemUpdating Event of DetailsView Control in ASP.Net.

protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
int idd =Convert.ToInt32(DetailsView1.Rows[0].Cells[1].Text);
TextBox name = DetailsView1.Rows[1].Cells[1].Controls[0] as TextBox;
TextBox city = DetailsView1.Rows[2].Cells[1].Controls[0] as TextBox;

SqlDataAdapter SQLAdapter = new SqlDataAdapter(“update UserMst set name='” + name.Text + “‘, city='” + city.Text + “‘ where id=”+idd+””, SQLConn);
DataTable DT = new DataTable();
SQLAdapter.Fill(DT);

DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
BindDetailsView();
}

How to Edit, Update, Detele in DetailsView in ASP.Net C#.
How to Edit, Update, Detele in DetailsView in ASP.Net C#.

The updated result is :

How to Edit, Update, Detele in DetailsView in ASP.Net C#.
How to Edit, Update, Detele in DetailsView in ASP.Net C#.

Now, we will learn to Delete Record from DetailsView in ASP.Net..

Here, set AutogenerateDeleteButton = True of DetailsView control.

detailview13

write code at ItemDeleting Event of DetialsView control for Delete record from DetialsView.

DetailsView in ASP.Net
DetailsView in ASP.Net

write below code at ItemDeleting event for Delete Record form Database….

 protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
int idd = Convert.ToInt32(DetailsView1.DataKey[e.RowIndex].ToString());

SqlDataAdapter SQLAdapter = new SqlDataAdapter(“delete from UserMst where id=” + idd + “”, SQLConn);
DataTable DT = new DataTable();
SQLAdapter.Fill(DT);

BindDetailsView();
}

I hope you enjoy with lean this ASP.Net Edit, update, delete Detialsview example…..

Leave a Reply

Your email address will not be published. Required fields are marked *