How To Create Out of Process Session State In SQL Server

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
  • Out-Proc

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:

  1. State Server: It use separate web server.

  2. 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.
  1. 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:

    command

    cmd

    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

  2. Create Web Project: WebSite or WebApplication.

    I had created web site project.

  3. Open Web.Config:

    Insert <sessionstate> inside - <configuration> <system.web> tag as given in image.

    Config
    1. <!-- You can use SQL server or Windows Connection string any one only.-->  
    2. <!-- Which one not required you can Comment or Remove it.-->  
    3.   
    4. <!-- Sql Server Connection String-->  
    5. <sessionState mode="SQLServer" sqlConnectionString="server=192.168.10.45\sa;User=sa;Password=mypassword;"/>  
    6.   
    7.   
    8. <!-- Windows Connection String-->  
    9. <sessionState mode="SQLServer" sqlConnectionString="server=192.168.10.45\sa;trusted_connection=yes" />  
  4. Create your page(s) as given in sample SqlServerOutProcSession.zip file.

  5. Run Application.

    • Screen 1 : Login.aspx

      login

    • Screen 2 : Home.aspx

      run

  6. 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.