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

3

5

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?

Mariann answered 25/7, 2011 at 21:2 Comment(0)
A
10

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;

    }
}
Amorete answered 25/7, 2011 at 21:30 Comment(0)
T
3

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.

Tjirebon answered 25/7, 2011 at 21:33 Comment(0)
I
-2

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 :)

Independency answered 25/7, 2011 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.