Geeks With Blogs

News Locations of visitors to this page
Ariel Popovsky's Blog Aventuras y desventuras con .net

This post was going to be a rant about how using the ObjectDataSource was impossible to handle exceptions thrown while retrieving or updating data. After some experiments and reading some misleading posts I saw only three possible ways:

  1. Use a generic error handler like the Page Error event or the Application Error event in global.asax.
  2. Implement handlers for the Updating, Selecting and Deleting events of the GridView bound to the datasource, set the event.Cancel to true and then manually call the Insert, Delete, Update and Select methods of the data source inside a try/catch block.
  3. Throw away the data source and do everything manually.

Luckily I accidentally found the right way to do it while looking at the EventArgs class properties for the Selected event. When the ObjectDataSource calls your data object, if the result is an exception it will call your post event event-handler (Selected, Deleted, etc). The EventArgs has two properties for managing exceptions, one is the Exception itself, the other is a boolean, ExceptionHandled. If you want to handle the exception you just need to set the ExceptionHandled property to true.

  1:         protected void MyDataSource_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
  2:         {
  3:             if(e.Exception!=null)
  4:             {
  5:                 ShowMessage(Constants.GenericErrorMessage, MessageType.Error);
  6:                 e.ExceptionHandled = true;
  7:             }
  8:             else
  9:             {
 10:                 ShowMessage("Entity deleted successfully.", MessageType.Success);                    
 11:             }            
 12:         }

 

Posted on Monday, June 22, 2009 10:54 AM ASP.net | Back to top


Comments on this post: Exception/Error handling using the ObjectDataSource

# re: Exception/Error handling using the ObjectDataSource
Requesting Gravatar...
THANK You! This is exactly what I needed.

I can catch any errors I want sent up from my business layer now.

And my page renders! No more blank error pages!

Left by Steve Gorham on Jun 17, 2010 12:25 PM

# re: Exception/Error handling using the ObjectDataSource
Requesting Gravatar...
Thanks! this really helped me
Left by Denisr Rodriguez on Sep 04, 2012 7:30 PM

# re: Exception/Error handling using the ObjectDataSource
Requesting Gravatar...
THANK YOU! SAVED ME **LOTS** OF TIME tracking and trying.
Left by Bruce on Oct 09, 2012 4:33 PM

Your comment:
 (will show your gravatar)


Copyright © Ariel Popovsky | Powered by: GeeksWithBlogs.net