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.

Hide
  • 115
  • 2
  • 11

1 Answers1

1

If you subscribe to your ObjectDataSource's selected event, you should be able to adequately handle the error there. This is explained here:

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

One of my primary complaints on SharePoint is there isn't a simple UnhandledException hook on webparts as there are on pages. It may be a bad design pattern, but in a decoupled system like SharePoint, it sure would be nice if one persons mistake didn't bring the whole thing down. /rant.

Community
  • 1
  • 1
Jaime Torres
  • 10,365
  • 1
  • 48
  • 56
  • Thx, I try looking for other solution but eventually I follow the solution in link you provided and if anything unexpected happens it works fine. Regards – Hide Oct 22 '13 at 09:39