Session
Session
Why Session
HTTP is a stateless protocol, it means every request is an independent request. Browser does
not know about previous request and data. So, every previous request data is lost and cannot
retrieve previous data. Basically using the session, we store the data in session and access it
anywhere as per requirement. It provides application level state management.
There are lot of other techniques that are used for state management such as
ViewState, Cookie, etc.
Session Timeout
1. Session's default timeout is 20 minutes.
2. We can set the session timeout manually in web.config or programmatically.
1. <configuration>
2. <system.web>
3. <sessionState timeout="30"></sessionState>
4. </system.web>
5. </configuration>
How to create a Session
Step 1
Coding
Output
Step 2
1. Create another webform page named as User (or choose the name you wish).
2. Write the following code at User.aspx.cs.
Coding
Session Mode
InProc
OutProc
1. SQL Server.
2. State Server.
SQL Server
1. Session data are stored in different Servers.
2. SQL Server database is used to store session data and SQL Server connects with
ConnectionString.
3. Write the following code at web.config.
1. <configuration>
2. <system.web>
3.
State Server
1. Session data are stored in different servers.
2. Sessions are stored using State Server windows service.
3. Write the following code at web.config
1. <configuration>
2. <system.web>
3.
Advantages of Session
Sessions are used to maintain the state of user data throughout the application. It stores any type of
object. Using the session, you can add variable values as well as any type of object such as object of
class, list, datatable, etc. It is secure.
Disadvantages of Session
It makes your website slow, if you use it. It is because the session value stores on server memory. So,
every time it need to serialize the data before storing it and deserialize the data before to show the
user.