How to Catch Errors when Using ObjectDataSource

The Situation:

You have an ASP.NET page that uses an ObjectDataSource to provide data access for a GridView, FormView or list control like a DropDownList or CheckboxList. When a data access error occurs, you would like to display a message on the page to help users understand what is happening.

A Solution:

You could try to handle the errors in the GridView or FormView by using the Updating, Selecting, or Deleting events, but perhaps you are in a position that the ObjectDataSource feeds data to more than one control on the page.

Instead of trying to handle the error in the consuming controls, implement the error handling in the ObjectDataSource itself using those same events. Here’s an example of what I mean:

<asp:ObjectDataSource ID="objCurrentRecord" runat="server" InsertMethod="InsertWidget"
	SelectMethod="GetWidgetByID" UpdateMethod="UpdateWidget" TypeName="AppWidgets"
	oninserted="objCurrentRecord_Inserted" onupdated="objCurrentRecord_Updated"
	ondeleted="objCurrentRecord_Deleted">
	...
</asp:ObjectDataSource>

 

And in the code-behind, implement the oninserted, onupdated, and ondeleted methods from above.

In the following example, my data access code throws a custom error when I try to insert a widget if the widget already exists, so I am checking for that error to provide feedback to the user. I have also enhanced the basic code to display a message upon successful insertion of the record.

protected void objCurrentRecord_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
	if (e.Exception != null)
	{
		if (e.Exception.InnerException.Message == "Widget already exists.")
		{
			lblResults.Text = e.Exception.InnerException.Message;
		}
		else
		{
			lblResults.Text = e.Exception.Message;
		}

		// MUST SET EXCEPTIONHANDLED TO TRUE so that the exception
		//    is not sent on to the calling control.
		e.ExceptionHandled = true;
	}
	else
	{
		lblResults.Text = "Widget Added.";
	}
}

 

Of course, you could do this in a variety of ways, the most flexible of which would be to create a method that reads the standard or custom error message and translates it into something that would make sense to the user.

Or even returning the standard error message itself like this:

protected void objCurrentRecord_Updated(object sender, ObjectDataSourceStatusEventArgs e)
{
	if (e.Exception != null)
	{
		lblResults.Text = e.Exception.Message;

		// MUST SET EXCEPTIONHANDLED TO TRUE so that the exception
		//    is not sent on to the calling control.
		e.ExceptionHandled = true;
	}
	else
	{
		lblResults.Text = "Widget Updated.";
	}
}

 

Returning the standard error message in a label is not very helpful to the end-user, but it may be better than sending them to an error page. Another way to handle this would be to simply display a “An error occurred when we tried to save your changes.” message.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s