ASP.NET - How to wrap GridView Delete in try catch or stop row delete
I want to be able to delete the gridview row in a try catch and display a nice error message on the screen, or try to stop deleting in certain circumstances.
I am currently getting foreign key violations in the database. So I either want to stop deleting if there are any child records or catch the foreign key exception and display a nice error message on the screen.
Can anyone tell me how to do this?
+2
source to share
2 answers
You can use GridView Row Deleting Event
Here's some sample code for that:
void CustomersGridView_RowDeleting
(Object sender, GridViewDeleteEventArgs e)
{
TableCell cell = CustomersGridView.Rows[e.RowIndex].Cells[2];
if (cell.Text == "Beaver")
{
e.Cancel = true;
Message.Text = "You cannot delete customer Beaver.";
}
else
{
Message.Text = "";
}
}
+4
source to share