0% found this document useful (0 votes)
8 views6 pages

Session

Uploaded by

Saravana Kohli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Session

Uploaded by

Saravana Kohli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SESSION

 Session is a state management technique.


 A session can store the value on the server.
 The session object is used to store and retrieve specific values within a web page and
it stores and retrieves the value of the particular time session.
 Session is one of the best techniques of State Management because it stores some
information or data and is used to pass it from one page to another.

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.

Session Timeout in programmatically


Session.Timeout = 10;

Session Timeout in web.config

1. <configuration>
2. <system.web>
3. <sessionState timeout="30"></sessionState>
4. </system.web>
5. </configuration>
How to create a Session
Step 1

1. Right-click on Solution Explorer then “Add”, select “Web Form”.


2. Create a webform page named as login (or choose the name you wish).
3. Create an event and write the following code at Login.aspx.cs.

Coding

1. protected void btnLogin_Click(object sender, EventArgs e)


2. {
3. Session["UserName"] = txtEmail.Text;
4. Session["PassWord"] = txtPassword.Text;
5. Response.Redirect("User.aspx");
6. }

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

1. protected void Page_Load(object sender, EventArgs e)


2. {
3. if (!IsPostBack)
4. {
5. if (Session["UserName"] != null)
6. {
7.

lblUser.Text = "Welcome to" + " " + Session["


UserName"].ToString();
8. }
9. }
10. }
Output

Session Mode

InProc

1. This is default session state mode.


2. Sessions are stored inside of the application's process on the webserver.
1. <configuration>
2. <system.web>
3. <sessionState mode="InProc"></sessionState>
4. </system.web>
5. </configuration>

OutProc

Session data are stored in different memory locations.

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.

<sessionState mode="SQLServer" sqlConnectionString=


"ConnectionString"></sessionState>
4. </system.web>
5. </configuration>

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.

<sessionState mode="StateServer" stateConnectionStr


ing="ConnectionString"></sessionState>
4. </system.web>
5. </configuration>

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.

You might also like