Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  




           


Handle Exception on ObjectDataSource bound to GridView
In ASP.NET, if an objectdatasource is bound to a gridview and throws an exception on databinding, the page crashes.
To intercept this and show a more friendly error message, attach a handler to the OnSelected event of the Data Source.
In the handler, check for an exception and set e.ExceptionHandled to true:

protected void odsData_Selected(object o, ObjectDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
LogHelper.Log(e.Exception, LogErrorLevel.WARN, true); // log the error
lblError.Text = "There was an error running this report. Execution failed: " + e.Exception.GetBaseException().Message; // show the error
e.ExceptionHandled = true; // stop page from crashing
}
}

Created By: amos 8/13/2013 11:18:10 AM
Updated: 8/13/2013 11:23:52 AM