ObjectDatasource的Select方法抛出异常如何处理?

发布于 2024-11-26 02:49:20 字数 221 浏览 1 评论 0原文

我有一个连接到 ObjectDatasource 的 Select 方法,该方法可能会引发异常,但我不知道如何处理它!

问题是我无法控制它。当页面呈现时,ObjectDatasource 直接调用 select 方法,并直接抛出未处理的异常。

另一方面,如果有问题,我不想让它返回空集合,因为集合可能是空的,没有问题。

那么,哪里可以处理异常呢?

还有其他选择吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羁绊已千年 2024-12-03 02:49:20

查看 ObjectDataSource 上的 eventargs。应该有一个 e.Exception & e.您可以查询选择成功/错误的结果。

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;

    }
}

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;

    }
}
半﹌身腐败 2024-12-03 02:49:20

您应该订阅 ObjectDataSource.Selected 事件。

<asp:ObjectDataSource OnSelected="ObjectDataSourceStatusEventHandler" />

正如 @Kirill 提到的那样,检查该事件中的异常,并可能隐藏 gridview 并向用户显示一些错误消息。检查此链接

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.

昔日梦未散 2024-12-03 02:49:20

如果我理解正确的话,您有一个页面在某个时刻调用 ObjectDataSource 上的 Select() ,并且此调用有时会失败并出现异常。

现在,您处理此异常的位置在某种程度上取决于您的场景。一般来说,您应该尽早尝试处理异常有意义的,也就是您可以做一些有用的事情来响应错误。例如,对于一个可能可以将用户重定向到错误页面的网站。

请注意,虽然这个有意义的早期点实际上可能已经很晚了,如果您将用户重定向到错误页面,它可能与 ui(或页面)层一样高。您可能会在早些时候尝试捕获异常并重试请求,如果失败,则重新抛出异常

抱歉 vauge awnser,但这实际上取决于:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文