Before reading this article, please go through the following article.
State Management Techniques in ASP.NET: Part 1
Here we will see some of the commonly used Server Side State Management techniques.
State management is required in ASP.NET since the web server treats each request independently and has no knowledge of the previous request. There are two types of state management techniques in ASP.NET, Client Side State Management and Server Side State Management. In the Server Side State Management the state is saved on the server whereas, in client-side state management, the state is stored on the client.
Session state
Session state is used to store the state specific to a particular session. The variables in the Session are stored in the Session collection, SessionStateItemCollection, and is exposed through the Session property. The session collection is indexed by a variable name or an integer index. Assigning the collection variable creates a new variable.
The following code stores the FirstName item in the session.
Session["FirstName"] = "Ashish";
We can store any data type in the session state. Here we are storing the dictionary object in the session.
Dictionary<string, string> names = new Dictionary<string, string>();
names.Add("FirstName", "ashish");
Session["Names"] = names;
If you want to store a complex data type in a session that is not an InProc session then please note that only serializable or primitive data types can be stored in an out of proc session.
A Session is identified through the session identifier. This session identifier is accessible through the SessionID property. The client can supply the session identifier using two ways, either in the cookie or in the URL. In the case of a cookieless session, the session identifier is stored in the URL. The various places where we can store session state are identified through the Session-State Modes.
We can use the SessionState element on the web. config to specify the session state settings. The following session state element specifies the values for a session state. Here we are specifying the sessionState mode to SQL Server.
<sessionState mode="SQLServer" cookieless="true"
regenerateExpiredSessionId="true" timeout="30"
sqlConnectionString="Data Source=Ashish;Integrated Security=SSPI;"/>
Application state
Application state is used to store the state common to the entire application, so it can be accessed from any page. We can store the common data that is frequently required by the application. Application state is stored in a dictionary, HttpApplicationState. So we store values in the application state using name value pairs.
The Application state is stored in the server memory so storing large amounts of data that is not required can degrade the application performance. Restarting the application or modifying the web. config causes the loss of the application state. If you want to store a large amount of data then consider using the cache, which is more efficient than the application state.
Since the application state can be accessed from multiple threads you should plan for synchronization while updating the application state. The following line stores the value "Ashish" in the name application state item.
Application["name"] = "ashish";
Profile Properties
Profile properties are used to store user-specific data in the application. The difference between session state and profile properties is that the data in the profile properties is persistent since it is stored in a data store such as the database. Various profile providers exist for working with the different data stores. In ASP.NET there is a SqlProfileProvider class that allows storing the data in the SQL Server database. We don't need to configure the database manually to use the Profile properties. To use the Profile we need to set the profile element in the web. config.
The following settings on the web. config can be used to set the FirstName profile property.
<profile>
<properties>
<add name="FirstName" />
</properties>
</profile>
The profile properties are maintained in the database and will be retrieved and set in the database just by setting the profile properties in the code. Profile properties are used to create a unique experience for the user. So the user preferences can be set using the profile properties and when the user visits the site again he will see his preferences already selected on the site.