.net 2.0 beta DetailsView question

  • Thread starter Thread starter jhoge123
  • Start date Start date
J

jhoge123

I've been going through the basic tutorials in .net 2.0 regarding the
creation of master/detail pages and using the DetailsView control to
edit records.

It's all pretty simple in the tutorials. But what if you want to check
for database errors? How do you put all the database interaction into a
try/catch block?

In an ideal world, all of the database constraints would be duplicated
in validation controls, so the form could only submit valid data. But I
would like to make sure that, if somehow the database rejected the
input, this could trigger a friendly error message to the user and
possibly an alert to the administrator.

Thanks,

John Hoge
 
For the various events of the DetailsView like ItemUpdated the EventArgs
parameter has an Exception and ExceptionHandled properties. This allows you
to inspect the exception that occurred and mark if it should be suppressed.

-Brock
DevelopMentor
http://staff.develop.com/ballen
 
Brock,

Thanks for your answer. I looked at the e.Exception object and saw that
the Message property could be parsed.

I have a table with a name column that has a unique constraint on it.
Theoretically the user could enter a duplicate name, and cause an
exception. I can see two ways to hand this:

1) In the ItemInserting event handler, query the database proactively
to see if this name being entered conflicts with other records in the
database and abort the insert if there is a conflict.

2) In the ItemInserted event handler, parse the message text for the
SQL constraint which will be violated in the event of a name conflict.
I would then write a friendly error message out and flag the Exception
as handled.

Option 1 is slower, but option 2 relies on parsing the error message,
which might not be super stable. Any guess?
 
Back
Top