French Boy
French Boy

Reputation: 1491

How to handle an exception is thrown by Select method of ObjectDatasource?

I have a Select method connected to an ObjectDatasource, this method might throw an exception and I don't know how to handle it!

The problem is that I'm not controlling it. When the page is rendered then the select method is called directly by the ObjectDatasource and an unhandled exception is thrown directly.

On the other hand, I don't want to make it return empty collection if it has a problem because the collection might be empty without problems.

So, where can I handle the exception?

Any other options?

Upvotes: 5

Views: 2696

Answers (3)

Kirill
Kirill

Reputation: 3088

Look at the eventargs on the ObjectDataSource. There should be an e.Exception & e.Results that you can query for the success/error of your select.

protected void MyOds_Selected (object sender, ObjectDataSourceStatusEventArgs e)
{
    if (e.Exception != null)
    {
        // handle exception here.
...
    //tell the ObjectDatasource that the exception is handled
    //and don't rethrow it.
    e.ExceptionHandled = true;

    }
}

Upvotes: 10

coder net
coder net

Reputation: 3475

You should subscribe to the ObjectDataSource.Selected event.

<asp:ObjectDataSource OnSelected="ObjectDataSourceStatusEventHandler" />

Check for exception in that event as @Kirill mentions and probably hide the gridview and display some error message to the user. Check this link.

Upvotes: 3

aL3891
aL3891

Reputation: 6275

If i understand correctly, you have a page that at some point calls Select() on a ObjectDataSource and that this call sometimes fails with an exception.

Now where you handle this exception is somewhat dependant on your scenario. in general you should try and handle exceptions at the earliest point where it makes sense, that is where you can do something useful in response to the error. For an website that might be at a point where you can redirect the user to an error page for example.

Note though that this early point where it makes sense might actually be quite late, in case you're redirecting the user to an error page, it might be as high up as the ui(or page) layer. You might at some earier point try to catch the exception and retry the request and if that fails, rethrow the exception

Sorry for the vauge awnser, but it really depends :)

Upvotes: -2

Related Questions