MANAGING PAGE LEVEL DATA IN SESSION

Sometime, int web application we need to store data in session excessively. This data probably not related to the current user and not being used through out the application. It may be process specific or page specific. The question arise here that if data is page specific than why to store in session, why can't it be stored in viewstate. Ofcourse, we can store data in Viewstate but storing large data in ViewState is not good idea because of following reason:

  • Data travels alongwith page HTML between client and server. If same data is bound to any grid or control, data travels in twice of the size.
  • If data is in user defined object or datatable/dataset, it requires to serialize and deserialize before stroring and after retrieving from the ViewState.

Above both cases are expensive with respect to performance and should always avoid. Always getting data from database or any service may also be expensive with respect to performance, so we are compelled to stored.

In most of the scenarios, SQL Server is used to manage the sessionstate. Anydata being stored in session must be serializable. For any object being stored/retrieved from session, serialization and deserialization happens respectively. All session data retrieved and stored back in sessionstate server for each request. It happens in following events.

  • Application_AcquireRequestState: This event occurs when session state being retrieved and stored in collection to use during the process of the page.
  • Application_ReleaseRequestState: Occurs when session state being stored back in the starage.

Therefore, if there are lots of data in session, each request process will impect the performance. Generally, I have seen that data stored in session to process a page not cleared after completion.

I am explaining a way to handle this situation and clear the unwanted session data pertaining to this scenario. We can create a class which keep the track of session variables.

    1public static class PageSessionManager
    2{
    3    public const string PAGE1 = "MyPage1";
    4
    5    public const string PAGE2 = "MyPage2";
    6
    7    private static string[] PageKeys;
    8
    9    static PageSessionManager()
   10    {
   11        PageKeys = new string[] { PAGE1, PAGE2};
   12    }
   13
   14    public static void CleanSessionExcept(string pageName_)
   15    {
   16        foreach (string pageKey in PageKeys)
   17        {
   18
   19            if (!pageName_.Equals(pageKey))
   20            {
   21                HttpContext.Current.Session.Remove(pageKey);
   22            }
   23        }
   24    }
   25    
   26}

Whenever you add new page and data has to store in Session for that specific page itself then create another property in this class and add it to PageKeys. When storing data in session, use respective page key on the page. Use the session how do you want to use. Write following code in Page_Unload event to clean the session for the data in session which not going to use.

    1protected void Page_Unload(object sender, EventArgs e)
    2    {
    3        PageSessionManager.CleanSessionExcept(PageSessionManager.PAGE1);
    4    }

v

Ebook Download
View all
Learn
View all