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.