0

I am creating a SharePoint WebPart which uses GridView and ObjectDataSource for retrieving data. When the connection is terminated (for different reasons) I want to catch exception and redirect user to page with information. I do not know if my WebPart will be placed in other WebParts or directly on a page.

I simulate the error by throwing Exception in the Select method of my class bounded to ObjectDataSource:

public List<Item> getItems(String param, int maximumRows, int startRowIndex)
{
    if (param == "a") throw new Exception("exception");

I can catch Exception whenever I invoke data binding in my code (example):

try
{
    gvMain.PageIndex = 0; //gvMian - SPGridView
    gvMain.DataBind();

}
catch (Exception ex)
{
    Page.Cache["cacheError"] = ex.Message;
    SPUtility.Redirect(SPUtility.GetPageUrlPath(HttpContext.Current) + "?wnd=err", SPRedirectFlags.Trusted, HttpContext.Current);         
}

but sometimes the page cannot catch exception (probably the data binding is invoked automatically) and shows server error with stack trace:

[Exception: exception]
   MyProject.odsClass.getItems(String param, Int32 maximumRows, Int32 startRowIndex) +211

[TargetInvocationException: Obiekt docelowy wywołania zgłosił wyjątek.]
   System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) +0
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +1255
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +38
   System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance) +897
   System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +1848
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +27
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +261
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
   System.Web.UI.WebControls.GridView.OnPreRender(EventArgs e) +46
   System.Web.UI.Control.PreRenderRecursiveInternal() +108
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3394

On this MSDN page I found a nice picture which recommends to catch this kind of exception on the WebPart boundary, but the example they provided is not clear to me (where would this boundary be in my WebPart code?)

I would be grateful for any suggestions and examples on how to catch this kind of exception, how to handle it in my code or for any suggestions on how to handle this issue in any other way.

4

1 回答 1

1

如果您订阅了 ObjectDataSource 的 selected 事件,您应该能够充分处理那里的错误。这在这里解释:

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

我对 SharePoint 的主要抱怨之一是 Web 部件上没有像页面上那样简单的 UnhandledException 挂钩。这可能是一种糟糕的设计模式,但在像 SharePoint 这样的解耦系统中,如果一个人的错误没有导致整个事情崩溃,那肯定会很好。/咆哮。

于 2013-10-18T17:42:44.913 回答