Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 909
French Boy Avatar asked Jul 25 '11 21:07

French Boy


2 Answers

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;

    }
}
like image 144
Kirill Avatar answered Nov 02 '22 12:11

Kirill


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.

like image 44
coder net Avatar answered Nov 02 '22 10:11

coder net