views:

263

answers:

1

I think I know the answer, but is it possible to have the global.asax Application_Error event modify a textbox on the original page and not move the client to a different page? something like: Exception exp = Server.GetLastError().GetBaseException(); System.Data.SqlClient.SqlException sqlex;

if (exp is System.Data.SqlClient.SqlException) {
  sqlex = (System.Data.SqlClient.SqlException) exp;
  if (sqlex.Number == 50000) {
    if (HttpContext.Current.CurrentHandler is Page) {
      Page p = (Page) HttpContext.Current.CurrentHandler;
      Control c = p.FindControl("ErrorText");
      if (c != null && c is Label) {
        ((Label) c).Text = exp.Message;
        Server.ClearError();
        return;
      }
    }
  }
}
+1  A: 

If you want to do this then you should use the "OnError" event of the page itself.

David
right.. the issue is that i want the exact same error handling for the entire application. Is there a way to use that code for all my page_OnErrors?
Rob
If you have a base class for all of your pages, you can handle it there. There is also a pageBaseType property in the web.config that might help you change the base class globally: http://msdn.microsoft.com/en-us/library/system.web.configuration.pagessection.pagebasetype.aspx
David