What is Session State and Types of Session State?
The session state is used to maintain the session of each user.
The session state is used to maintain the session of each user throughout the application.
There are two ways to store session,
IN-PROC: By default all sessions are getting stored in IN-PROC.
OUT-PROC: You can store session in OUT-PROC in the following two ways:
- State Server: It use separate web server.
- SQL Server: It use SQL Server database can be: Custom Database or TempDb (System).
Step by Step:
I am using Microsoft Visual Studio Ultimate 2012.
- To create ASPState database in desired location of SQL Server:
You can access SQL Server with two different ways: Windows authentication or SQL Server authentication.
To execute the following command:
Windows Authentication: To create ASPState:
Command: aspnet_regsql –S 192.178.10.50\sa –E –ssadd
Sql Server Authentication: To create ASPState.
Command: aspnet_regsql –S 192.168.10.50\sa –ssadd –U sa –P mypassword
By default above command create table inside
- Create Web Project: WebSite or WebApplication.
I had created web site project.
- Open Web.Config:
Insert <sessionstate> inside - <configuration> <system.web> tag as given in image.
- <!-- You can use SQL server or Windows Connection string any one only.-->
- <!-- Which one not required you can Comment or Remove it.-->
-
- <!-- Sql Server Connection String-->
- <sessionState mode="SQLServer" sqlConnectionString="server=192.168.10.45\sa;User=sa;Password=mypassword;"/>
-
-
- <!-- Windows Connection String-->
- <sessionState mode="SQLServer" sqlConnectionString="server=192.168.10.45\sa;trusted_connection=yes" />
- Create your page(s) as given in sample SqlServerOutProcSession.zip file.
- Run Application.
- Screen 1 : Login.aspx
- Screen 2 : Home.aspx
- While call the value of session for display in Home.aspx page.
Please, convert into string with (Convert.ToString) or Serialize.
Otherwise, you will face problem.
Happy coding!
Also Refer this link to know in depth.