Sergey Smelov
Sergey Smelov

Reputation: 1081

How to catch exception occured in DAL of ObjectDataSource object?

I have ObjectDataSource and GridView on one of the pages of my asp.net web project.

To initialize ObjectDataSource i use Init event:

   protected void ObjectDataSource1_Init(object sender, EventArgs e)
    {
            ObjectDataSource1.EnablePaging = true;
            ObjectDataSource1.TypeName = "CustomerDataSource";
            ObjectDataSource1.DataObjectTypeName = "Customer";
            ObjectDataSource1.SelectMethod = "GetCustomers";
            ObjectDataSource1.UpdateMethod = "Update";
            ObjectDataSource1.SelectCountMethod = "GetCustomersCount";
            ObjectDataSource1.MaximumRowsParameterName = "maximumRows";
            ObjectDataSource1.StartRowIndexParameterName = "startRowIndex";

      }

Somewhere in DAL:

public static List<Customer> GetCustomers(int maximumRows, int startRowIndex)
{
   try {
         ... some select to database here...
       }
       catch (Exception ex)
       {
         Utils.LogError(ex);
         throw ex;
       }
}

Now let's imagine that GetCustomers for some reason throws an exception. How could i catch this exception and handle it? I know i can use Application_Error, but i would like to catch this exception somehow on my page and show some friendly message to the user. Is that possible?

Thanks

Upvotes: 1

Views: 990

Answers (1)

to StackOverflow
to StackOverflow

Reputation: 124726

You can try handling the relevant ObjectDataSource event.

In your example, the Selected event, but there are also Inserted, Updated and Deleted events that you handle in the same way.

The events provide an ObjectDataSourceStatusEventArgs which has the Exception, and a property ExceptionHandled that you can set to indicate you have handled the exception (for example, displaying an error message).

There are some samples in MSDN.

Upvotes: 3

Related Questions