TheChaseMan's Frenetic SoapBox

Always looking for better ways to do things...

ObjectDataSource Control: Handling Exceptions

I've really been impressed with the ObjectDataSource control. Not only does it give you a very intuitive way to provide data binding with your custom object model, but I found that it had a really useful event model as well. For example, let's say I have a custom ASP.NET error control that gets passed an exception and the control then displays a friendly error message to the user depending on the exception type. But what happens if the ObjectDataSource is bound to a select method that ends up throwing an exception and I want to handle it on my UI layer? Fortunately, the ObjecctDataSource control has an event named Selected and another event named Selecting. Using the Selected event, I can test the ObjectDataSourceStatusEventArgs to see if an exception has been thrown, handle it, and then exit gracefully by setting an ExceptionHandled flag to true...

//ObjectDataSource Select method bound here...

public List<Person> GetPeople() {

    List<Person> people = new List<Person>();

 

    //force exception here for example...

    throw new InvalidOperationException();

 

    return people;
}
 

//Page code-behind here...

protected void ObjectDataSourcePeople_Selected(object sender, ObjectDataSourceStatusEventArgs e) {

    if (e.Exception != null) {

        //Handle the exception here...

        e.ExceptionHandled = true;

    }

}
 
Very cool stuff!
Digg!

posted on Saturday, November 12, 2005 2:33 PM

Feedback

# re: ObjectDataSource Control: Handling Exceptions 3/20/2007 8:06 AM Siva

Hi,
Question for you. I tried this from my code; I have a business object layer where , I have added to code to throw an exception when it encounters one. I catch that exception and handle it in my UI layer where I use the object datasource.
The problem is, while in the business layer method, the eception message is more detailed (for instance, "Connectivity to SQL server can not be established") when I throw the same excption to UI layer, and in the UI layer inside of the method "ObjectDataSource1_Selected", when I try to handle the exception the message I retrieve out of it using the e.Exception.InnerException.Message is pretty generic - like "Exception of type system.exception " was thrown which is not acceptable. IS there anyway to accurately retireve the more detailed message which was present in the original execption which was thrown?
Thanks
-Siva