A session object can persist information between HTTP requests for a particular user, whereas an application object can be used globally for all the users.
You can change the timeout for a Session object by setting its Timeout property or by using the Web.Config file.
- When you want to pass value/object to another page you can use this session object.
- When you want to save (temporary) some data for that particular user , you can use this session object. Eg., Passing login information of the user from one page to another.
The Session objects are non-persistent and it is not strongly typed. The Session objects uses the In Proc, Out Of Process or SQL Server Mode to store information. It is not only allowed for authenticated users but also allows to the unauthenticated users.
Session[“uname”]=TextBox1.Text and Session.Add(“uname”,TextBox1.Text) are both used to assign data from the TextBox1 to a key known as “uname”. Both commands will create a row in the Session Object.
Session[“uname”]=TextBox1.Text ; It uses an indexer to assign the data. The indexer refers to the particular row of the session object. The key “uname” is used to identify the row.
Assigning data using an indexer is faster and more efficient than assigning the data using the method–Session.Add(“uname”,TextBox1.Text)
For any web programmer, its obvious to think and believe that SessionId remains same through out the user session and it was right till asp.net1.1. But in asp.net2.0, this behavior has changed. In the asp.net application new sessionid is returned with the response to every request until session objects are not used.
According to MSDN the reason/solution is:
“When using cookie-based session state, ASP.NET does not allocate storage for session data until the Session object is used. As a result, a new session ID is generated for each page request until the session object is accessed. If your application requires a static session ID for the entire session, you can either implement the Session_Start method in the application’s Global.asax file and store data in the Session object to fix the session ID, or you can use code in another part of your application to explicitly store data in the Session object.”