Configure sqlserver session state on asp.net

In asp.net sessions can be enabled on three modes. In-Process is the default mode in which sessions values are managed on the asp.net process itself. Sqlserver mode enables to store session variables on the server server database itself so they are persistent even after the full server restart.

Configure Database

Run the commad prompt.
Switch to the following location on command prompt. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 Run aspnet_regsql command.

You will be presented with a wizard to install the necessary objects on the database to enable your database to store session related data from your asp.net application. You can use the same database that you are using for the application to store session data.


aspnetregsql_1.png

Introduction Screen - Click next to continue...


Select Option

Select the option "Configure SQL server for application services". and click Next. Another option removes the application services from the database if you have previously installed the application services. Since you are going to add services click the first option.


aspnetregsql_3.png

Now provide the database server, provide valid user name and password and select the database schema and click next to continue.....


Confirm Screen

Confirm you settings and continue to next


Success screen

If everything goes well you got the final success screen like this.

The above wizard configures sqlserver database that stores information for asp.net application services(membership, profiles, roles,personalization, sql web event provider). If you want to add additional features for sqlserver session state, use command line options as below. aspnet_regsql -ssadd -U sa -P sa -S localhost\sqlexpress -d databasename -sstype c

Configure on the web.config


Xml configuration file example

Now time to configure on web.config. Configure the session state mode to sqlserver and provide the valid connectionstringname.

Now your application is sqlserver session mode enabled. Now you will not be logged out every time when u change your code.This saves your lots of time while debugging your application if your application is session dependent.

Advantages of sqlserver mode.

Sqlserver mode has various advantages. The main advantage is the session values are persitant even if the web server restarts. Another good reason to use sqlserver mode is to make session variables accessible across the multiple web server farms. 

 

blog comments powered by Disqus