locked
Session in DB problem RRS feed

  • Question

  • User-1886006708 posted

    Hello All,

    In our application we have persisted the Session in database. Everything works fine. Now the problem that we are facing is that

    If the database server we are connecting to is down for some reason then we want to show a graceful error on the Error Page. We have handled the Page_Error event in the Basepage and when the exception occurs then this event also catches the error. Here is the Code.

    protected void Page_Error(object sender, System.EventArgs e)
        {
            Exception ex = Server.GetLastError();
            CustomException mobjBE = new CustomException();
            Exception modifiedEx = mobjBE.SetErrorDescription(this.UserId, ex);
    
            string lPage = Request.ApplicationPath + "/Common/errmessage.aspx?errMsg=" + modifiedEx.Message;
    
            Session["LastErrorException"] = modifiedEx;
            Response.Redirect(lPage);
        }

    But on the Response.Redirect it again throws the same error. (We dont know why). The exception is actually the TRANSPORT LEVEL EXCEPTION OF SQL SERVER .... 

     

    Then i thought that the errormessage.aspx page might be the problem because it was inherited from the Basepage.(The basepage  tries to connect to the Database). I made a new page that was not inherited from the base and also was not using any master page. But the same Transport level exception came from the Response.Redirect line of the code.

     

    Then i tried Global.asax. It has an event void Application_Error(object sender, EventArgs e)  and i tried to catch the error and it worked then i redirected it to my new error page but the same exception still came and it became a recursive call.

     

    Kindly, help me i have tried everything that i knew. 

     

    Regards,

    Behroz Sikander

    Friday, April 29, 2011 3:55 AM

Answers

  • User533502567 posted

    Session["LastErrorException"] = modifiedEx; Response.Redirect(lPage);

    After that event ASP.NET still considers Exception as not handled, and handles it by logging to the Application Event Log and interrupting normal event life cycle, which in turn prevents to save the latest Session state changes. Change code to..

     Session["LastErrorException"] = modifiedEx;
     Response.Redirect(lPage,false);
     Server.ClearError();  
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 29, 2011 9:44 AM
  • User-481631678 posted

    Always use Server.ClearError (); whenever you want to stop the error propagation as mentioend by budugu as well. Also Try writing code in Application_Error event which is a more appropriate place to handle application wide erros. For details see this wonderfull tutorial:-

     

    http://aspnetresources.com/articles/CustomErrorPages

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 3, 2011 4:31 AM
  • User-519136805 posted

    Session["LastErrorException"] = modifiedEx;

    Above statement cause the problem.

    Problem is that you are getting Session related error (session state = "SQL Server" , you DB is not connected) , still you are saving data in session, so same error comes.

    you can just log the error & redirect to error page & show some common user friendly message rather than showing actual error to user, there is no point to show technical error description to end users.

    Hope this help !!!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 3, 2011 9:33 AM

All replies

  • User377791177 posted

    Response.Redirect won't throw any SQLException it must be the redirected page itself, you must check at all the places where the database code is called.

    Friday, April 29, 2011 4:32 AM
  • User-1886006708 posted

    I know but doest it do something with Session ?

    Friday, April 29, 2011 9:26 AM
  • User533502567 posted

    Session["LastErrorException"] = modifiedEx; Response.Redirect(lPage);

    After that event ASP.NET still considers Exception as not handled, and handles it by logging to the Application Event Log and interrupting normal event life cycle, which in turn prevents to save the latest Session state changes. Change code to..

     Session["LastErrorException"] = modifiedEx;
     Response.Redirect(lPage,false);
     Server.ClearError();  
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 29, 2011 9:44 AM
  • User-481631678 posted

    Always use Server.ClearError (); whenever you want to stop the error propagation as mentioend by budugu as well. Also Try writing code in Application_Error event which is a more appropriate place to handle application wide erros. For details see this wonderfull tutorial:-

     

    http://aspnetresources.com/articles/CustomErrorPages

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 3, 2011 4:31 AM
  • User-519136805 posted

    Session["LastErrorException"] = modifiedEx;

    Above statement cause the problem.

    Problem is that you are getting Session related error (session state = "SQL Server" , you DB is not connected) , still you are saving data in session, so same error comes.

    you can just log the error & redirect to error page & show some common user friendly message rather than showing actual error to user, there is no point to show technical error description to end users.

    Hope this help !!!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 3, 2011 9:33 AM