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

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

The problem is that I do not control it. When the page is displayed, the select method is called directly by the ObjectDatasource, and an unhandled exception is thrown directly.

On the other hand, I do not want it to return an empty collection if it has a problem, because the collection can be empty without problems.

So where can I handle the exception?

Any other options?

+4
source share
3 answers

Look at eventargs on an ObjectDataSource. There should be an e.Exception and e.Results exception that you can request for the success / error of your choice.

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; } } 
+9
source

You must subscribe to the ObjectDataSource.Selected event.

 <asp:ObjectDataSource OnSelected="ObjectDataSourceStatusEventHandler" /> 

Check for an exception in this event, as @Kirill mentions and probably hides the gridview and displays an error message to the user. Check out the link.

+2
source

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

Now that you are handling this exception, it depends to some extent on your scenario. in general, you should try and handle exceptions at the earliest point where it makes sense, here is where you can do something useful in response to an error. For a website, which may be at the point where you can redirect the user to the error page, for example.

Please note that this early point where it makes sense can be pretty late if you redirect the user to a page with an error, it can be as high as the level of ui (or page). You may at some earlier point try to catch the exception and retry the request, and if that fails, restore the exception

Sorry for awnser vauge, but it really depends :)

-1
source

All Articles