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:



    Windows Authentication: To create ASPState:

    Command: aspnet_regsql –S\sa –E –ssadd

    Sql Server Authentication: To create ASPState.

    Command: aspnet_regsql –S\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.

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

  5. Run Application.

    • Screen 1 : Login.aspx


    • Screen 2 : Home.aspx


  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.

Up Next
    Ebook Download
    View all
    View all