Managing Data in Session for Page Level Data

 18-Aug-2012   NityaPrakash     ASP.NET  SessionState    Comments  0

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. Any data 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.

public static class PageSessionManager
    public const string PAGE1 = "MyPage1";

    public const string PAGE2 = "MyPage2";

    private static string[] PageKeys;

    static PageSessionManager()
        PageKeys = new string[] { PAGE1, PAGE2};

    public static void CleanSessionExcept(string pageName_)
        foreach (string pageKey in PageKeys)

            if (!pageName_.Equals(pageKey))

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.

    protected void Page_Unload(object sender, EventArgs e)

Nitya Prakash Sharma has over 10 years of experience in .NET technology. He is currently working as Senior Consultant in industry. He is always keen to learn new things in Technology and eager to apply wherever is possible. He is also has interest in Photography, sketching and painting.

My Blog
Post Comment