State Management in Asp.Net
As we know that applications developed using ASP.NET depends on the protocol such as HTTP, a stateless protocol. If the user is trying to register a form by filling the user details and when the user clicks on the submit button, the information will be lost because ASP.NET supports stateless protocol.
For post back requests, the web application or webpage does not hold the previous user's information. In order to overcome the above problem, we have an alternative that is State Management technique.
State Management
It is a process of storing the user's information when user submits the request to the Web Server. We have two types of State Management techniques.
As we know that applications developed using ASP.NET depends on the protocol such as HTTP, a stateless protocol. If the user is trying to register a form by filling the user details and when the user clicks on the submit button, the information will be lost because ASP.NET supports stateless protocol.
For post back requests, the web application or webpage does not hold the previous user's information. In order to overcome the above problem, we have an alternative that is State Management technique.
State Management
It is a process of storing the user's information when user submits the request to the Web Server. We have two types of State Management techniques.
- Server Side State Management
- Client Side State Management
Server Side State Management
When the user submits its request (like user registration), then the user's information will be stored with the Web Server. If the user data is confidential, then we need to make use of Server Side State Management to save the user's data into the Web Server memory.
We can implement Server Side State Management technique in two ways.
When the user submits its request (like user registration), then the user's information will be stored with the Web Server. If the user data is confidential, then we need to make use of Server Side State Management to save the user's data into the Web Server memory.
We can implement Server Side State Management technique in two ways.
- Session State Management
- Application State Management
To implement Session state management technique, we will use a concept of session and which is a part of HttpSessionState class. Session is a variable which is used for storing the user information or user's data.
As we know that in Server side state management, we will store the user's data / user's information into the Web Server memory. Within the Web Server's memory, a session variable will be created in order to store the user data using session state management.
The session will be available in the Web Server's memory until they are removed or the session expires. Each and every session will be representing with a unique id which can be called as Session id. In order to access the user 's data which is stored in the Web Server memory, we can access it by session id.
When the user requests for a webpage, then on behalf of every user, the session will be created and different session variables will be maintained for different users who request the webpage. Web Server will maintain all the session variables in its memory, when the user requests for a webpage. The default lifetime of the session variable will be 20 minutes. If we want to increase the life time of variable, then we have to use the timeout attribute.
<Session state mode="InProc" timeout="60"> (Here in proc is the default mode and 60 is the minutes.)
Example
- Session["Value"]= Textbox1.Text;
- Label1.Text=Session["value"];
State Providers
It is the place or location where the session variables are created. It is part of the Web Server. In ASP.NET, we have different types of sessions.
It is the place or location where the session variables are created. It is part of the Web Server. In ASP.NET, we have different types of sessions.
- In proc session
- State server session
- SQL server session
- Custom session
For defining the session, we need to configure the settings in web.config file like below.
- <configuration>
- <System.web>
- <Session state mode="Inproc(default), Timeout=" 60 ">
- Custom,
- off,
- Sql server,
- state server" </Session state>
- </System.web>
- </configuration>
Here, in the above configuration file, "In proc" is the default mode of session state management; and if mode="off", then that application will not support any kind of session.
- Mode It is an attribute for defining the type of the session.
- Timeout Timeout attribute defines the lifetime of the session given by the programmer in minutes.
In proc session
The "In proc" session will be creating within the current app domain and its part of the Web Server. It is the default mode of the session state. It supports two kinds of events, such as Session_start and Session_End events. The session_start event fires when the new session variable is created.
The session_end event fires when the session is expired or session ends. From the above four sessions, like In proc, state, SQL server, and custom, only In proc session supports session_end event. Accessing the In proc session will be faster compared to other sessions because it is part of Web Server.
The "In proc" session will be creating within the current app domain and its part of the Web Server. It is the default mode of the session state. It supports two kinds of events, such as Session_start and Session_End events. The session_start event fires when the new session variable is created.
The session_end event fires when the session is expired or session ends. From the above four sessions, like In proc, state, SQL server, and custom, only In proc session supports session_end event. Accessing the In proc session will be faster compared to other sessions because it is part of Web Server.
If the web Server is started or crashed, the "In proc" session data may loose. So, it is suitable for small web applications.
Example for In proc Session
- <configuration>
- <system.web>
- <sessionState mode="InProc" timeout="50"> </sessionState>
- </system.web>
- </configuration>
Login.aspx.cs
- class Login {
- Public void Loginclick() {
- Session["userid"] = Textlogin.Text;
- Response.Redirect("Welcome.aspx");
- }
- }
Welcome.aspx.cs
- Class Welcome {
- pblic void Page_Load() {
- Label1.Text = Session["userid"].ToString();
- }
- }

Khaja MoizuddinPosted Feb 4, 2017, 3:43 AM
Thanks everyone for your response........ :)
Anil KumarPosted Feb 2, 2017, 6:19 AM
Nyc article. thanks for sharing
Manav PandyaPosted Feb 2, 2017, 12:08 AM
Thanks for sharing such a important article sir Khaja Moizuddin