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?
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;
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With