The Self-Taught Programmer

Recipes from real-world experience with asp.net.

Posts Tagged ‘exception handling

How to Catch Errors when Using ObjectDataSource

leave a comment »

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.

Advertisements

Written by Jim McMullen

October 19, 2012 at 12:15 pm

Posted in How-To

Tagged with ,

Elmah Signaling – How To Log Exceptions with Elmah but Continue Processing

leave a comment »

Elmah is an excellent tool for web programmers. It is an extremely easy to install exception handler that logs application exceptions and sends the user to an “oops” page. It can also send an email to the developer or webmaster.

But what if you want to log an error but NOT send the user to an “oops” page. This can be especially useful in debugging code that already handles exceptions so that the user doesn’t see them.

Here is how it works:

Add a reference to Elmah to the page:

using Elmah;

Then, in your try/catch block, use this code:

try
{
    //some code}
catch (Exception ex)
{
    // handle your exception here
    ErrorSignal.FromCurrentContext().Raise(ex); //ELMAH Signaling  (logs error but processing continues)
    return false;  //assuming the method returns a boolean value.
}

Written by Jim McMullen

May 5, 2011 at 3:18 pm

Posted in How-To

Tagged with , ,