Update: It turns out that the browser I was using (Avant) was swallowing some of the errors and in some cases just behaving badly, even though it's supposed to be just a shell for IE. I've tried everything in IE proper and, following David's article, exceptions during delete are caught in the List page.
However, in the Listdetails page the problem still exists. Further, trying to set the CausesValidation property of the delete link buttons in the code-behind results in an error - "Setting CausesValidation on DataControlButtons is not supported.". The documentation says that the property is true by default but I've written code to spit out its value and it apparently is set to false by the framework. Further still, I added a template column to the gridview with my own delete link button, whose CausesValidation property was set to true. When I clicked on my button, a ValidationException I forced is caught and shown in the validation summary. However, database errors aren't shown.
Here is the code to my partial method in the datacontext
partial void DeletePoliceman(Policeman instance)
{
//throw new ValidationException("Can't delete policeman");
try
{
ExecuteDynamicDelete(instance);
}
catch (Exception e)
{
throw new ValidationException("Can't delete policeman2", e);
}
}
If I uncomment the first line where an exception is unconditionally thrown, the message appears properly in the validation summary. However, I then commented that line, and depended on the second throw instance in the catch block to alert me to database errors. When I tried deleting a policeman record that had child records associated with it, the database error seemed to bypass the catch block, because I got the default error message - "The DELETE statement conflicted with the REFERENCE constraint", instead of ,u custom message I was expecting. So, my questions:
- Can the CausesValidation property be set to true for the delete link buttons in the gridview (and the detailsview) of the ListDetails template page?
- How can I trap database exceptions?