Ariel Popovsky's Blog

Aventuras y desventuras con .net
posts - 7, comments - 59, trackbacks - 0

My Links

News

Locations of visitors to this page

Twitter












Tag Cloud

Archives

Post Categories

My Sites

Exception/Error handling using the ObjectDataSource

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:         }

 

Print | posted on Monday, June 22, 2009 10:54 AM | Filed Under [ ASP.net ]

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: