Microsoft KB Archive/317604

= HOW TO: Configure SQL Server to Store ASP.NET Session State =

Article ID: 317604

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft SQL Server 2000 Standard Edition
 * Microsoft SQL Server 7.0 Standard Edition
 * Microsoft SQL Server 2000 64-bit Edition

-



This article was previously published under Q317604



IN THIS TASK
SUMMARY
 * Requirements
 * Configure SQL Server for ASP.NET SQL Server Session State
 * Modify the Web.config File of Your Application
 * Troubleshooting

REFERENCES



SUMMARY
This step-by-step article demonstrates how to configure Microsoft SQL Server for ASP.NET SQL Server mode session state management.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows 2000 Advanced Server, or Microsoft Windows XP
 * Microsoft .NET Framework
 * Microsoft Internet Information Services (IIS)
 * Microsoft SQL Server

back to the top

Configure SQL Server for ASP.NET SQL Server Session State
The following steps describe how to run the InstallSqlState.sql and the UninstallSqlState.sql script files to configure SQL Server mode session state management.  In SQL Query Analyzer, on the File menu, click Open. In the Open Query File dialog box, browse to the InstallSqlState.sql script file, and then click Open. By default, InstallSqlState.sql is located in one of the following folders:

\WINNT\Microsoft.NET\Framework\ \

\Windows\Microsoft.NET\Framework\

 After InstallSqlState.sql opens in SQL Query Analyzer, click Execute on the Query menu to run the script. Before you run the UninstallSqlState.sql script file to uninstall SQL Server mode session state management configuration, you must stop the w3svc process. To do this, follow these steps:  On the Windows Start menu, click Run, type cmd, and then click OK to open a command prompt. At the command prompt, type net stop w3svc. You receive confirmation that the w3svc process is stopped.</ol> </li> In SQL Query Analyzer, on the File menu, click Open.</li> In the Open Query File dialog box, browse to the UninstallSqlState.sql script file, and then click Open. By default, UninstallSqlState.sql is located in one of the following folders:

\WINNT\Microsoft.NET\Framework\ \

\Windows\Microsoft.NET\Framework\

</li> After UninstallSqlState.sql opens in SQL Query Analyzer, click Execute on the Query menu to run the script.</li> After you uninstall SQL Server mode session state management configuration, you must restart the w3svc service. To restart the w3svc process, type net start w3svc at a command prompt.</li></ol>

back to the top

Modify the Web.config File of Your Application
To implement ASP.NET SQL Server mode session state management, you must modify the <sessionState> element of your application's Web.config file as follows: <ol> Set the mode attribute of the <sessionState> element to SQLServer to indicate that session state is stored in SQL Server.</li>  Set the sqlConnectionString attribute to specify the connection string for SQL Server. For example: sqlConnectionString=&quot;data source=MySQLServer;user id= ;password= &quot;

Note The user,, must have permissions to perform this operation on the database.

The modified <sessionState> element should appear as follows: <sessionState mode=&quot;SQLServer&quot; sqlConnectionString=&quot;data source=127.0.0.1;user id= ;password= &quot; cookieless=&quot;false&quot; timeout=&quot;20&quot; /> Note Ensure that you use the correct case when you specify the <sessionState> element and the associated attribute values. This code is case sensitive. </li></ol>

back to the top

Troubleshooting
<ul> If you do not stop the w3svc process before you run the UninstallSqlState.sql script file, you receive the following error message:

Cannot drop the database 'ASPState' because it is currently in use

</li> If entries in the ASPStateTempSessions table are not removed after the related sessions expire, make sure that the SQL Server agent is running. You can implement this functionality through stored procedures that are scheduled through jobs in SQL Server. The SQL Server agent manages these jobs.</li> When you use the default InstallSqlState.sql and UninstallSqlState.sql script files to configure ASP.NET SQL Server mode session state management, note that these files add the ASPStateTempSessions and the ASPStateTempApplications tables to the tempdb database in SQL Server by default. Furthermore, if you restart SQL Server, you lose the session state data that was stored in the ASPStateTempSessions and the ASPStateTempApplications tables.For additional information about how to run alternative scripts to configure persistent SQL Server session state management so that the session data is not lost when you restart the server, click the article number below to view the article in the Microsoft Knowledge Base:

311209 HOW TO: Configure ASP.NET for Persistent SQL Server Session State Management

</li></ul>

back to the top

<div class="references_section">