Session state is one of the popular state management techniques in ASP.NET environment. We developer people play with session storage every now and then. It’s pretty simple to manage session if you understand the basic concept. Here is the syntax to do that.
Session[“KEY”] =”Value”;
Or
Session[index] =”Value”; .
That’s very fine when we are in code behind of Web Form application. But when we will implement session in service based application. (Let’s think WebMethod in code behind which must be defined as static if we want to invoke using JQuery ajax()) the syntax is bit different.
HttpContext.Current.Session[“KEY”] =”Value”;
or
HttpContext.Current.Session[index] =”Value”;
This small discussion is enough to understand the syntax to deal with session variable in C#/ASP.NET. Let’s come to our main discussion. What is the difference between HttpContext.Current.Session and HttpContext.current.Item.
If it is question in interview then the single line answer is “ HttpContext.Current.Item” data is live for single HTTP request/Response where HttpContext.Current.Session data is live throughout user’s session.
So, HttpContext.Current.Item is short term storage. Means ,data store in this variable for shortest time period. Items collection in HttpContext is an IDictionary key-value collection and that are shared across single HTTP request.
Once the server finish the processing of data which is stored in Items[] and send back result in browser, the data flush automatically(yes, without any external event).
Here is syntax to save data in Items[] collection.
HttpContext.Current.Items["Value"] = "This is value";
And in case of data retrieval, we can use below syntax.
HttpContext.Current.Items["Value"]
But, in case of Session[] collection the data store during current session of user.
Let’s implement the concept in example. Here we will compare both Session[] collection with Items[] collection.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApp
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
HttpContext.Current.Items["Value"] = "Sourav Kayal in ITEM";
HttpContext.Current.Session["Value"] = "Sourav Kayal in SESSION";
Response.Write((string)(HttpContext.Current.Items["Value"]) + "<br>");
Response.Write((string)(HttpContext.Current.Session["Value"]));
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write((string)(HttpContext.Current.Items["Value"]) + "<br>");
Response.Write((string)(HttpContext.Current.Session["Value"]));
}
}
}
The trick, which we have adopted in this example, is very simple. In page loading time we are checking whether the page is loading at first time or not? If the page loads at first time we are initializing both Session[] and Items[] collection and we are printing both data in next two lines. This gives below output.
So, in time of collection initialization both session and Items are active and we are getting the value what we have set it.
Now, n second run we will press the button which we have placed in page. The trick is that, after button pressing the page will get post back but the content of within if condition in Page_Load() method will not execute. Because the page is loading by post back . So, the session and Items will not initialize in second time.
Then control will execute the click event of button, where we are trying to print value of both Session and Items collection by below code.
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write((string)(HttpContext.Current.Items["Value"]) + "<br>");
Response.Write((string)(HttpContext.Current.Session["Value"]));
}
And we are getting value only from Session collection not from Items collection. The session is at the time of page load( first load) the Items collection was populated and the result came back in browser and after that it got flash where session data is still alive in post back operation.
Fine, we have understood the basic difference between Session and Collection in ASP.NET. Now, the question is in which scenario Items[] collection is best to use ?
As Items[] life is within single HTTP request, we cannot use it to store user’s data in subsequent request.
One of the best places to use Items collection is to pass data from between HttpModule and HttpHandler. As we know that each and every HTTP request passes through HttpModule and HttpHandler to make communication between then we can use Items collection and when the HTTP request will pass through, the Items collection will flash automatically.
The another possible place to use Items collection when you are sharing same information across the different instance based on the user request and the request could be change for different request.
So, those are the possible situation where Items collection comes in picture. The fact should keep in mind that, Items store data as object so proper type casting is needed to get actual result from Items collection.
Summary
The main difference between Session and Items is it’s lifetime. I hope session is not now to you and you know the life of session variable pretty well. The life of Items collection is very short and it only cover single HTTP request.