Amritpal Singh
Amritpal Singh

Reputation: 1785

Asp.Net 4.0 Storing Session in SqlServer

I Have Database provided by shared hosting. I want to store the session in sql server but it give me error:

Unable to use SQL Server because ASP.NET version 2.0 Session State is not installed on the SQL server. Please install ASP.NET Session State SQL Server version 2.0 or above

[HttpException (0x80004005): Unable to use SQL Server because ASP.NET version 2.0 Session State is not installed on the SQL server. Please install ASP.NET Session State SQL Server version 2.0 or above.]
System.Web.SessionState.SqlPartitionInfo.GetServerSupportOptions(SqlConnection sqlConnection) +2147087
System.Web.SessionState.SqlPartitionInfo.InitSqlInfo(SqlConnection sqlConnection) +107
System.Web.SessionState.SqlStateConnection..ctor(SqlPartitionInfo sqlPartitionInfo, TimeSpan retryInterval) +531
System.Web.SessionState.SqlSessionStateStore.GetConnection(String id, Boolean& usePooling) +237
System.Web.SessionState.SqlSessionStateStore.CreateUninitializedItem(HttpContext context, String id, Int32 timeout) +136
System.Web.SessionState.SessionStateModule.CreateUninitializedSessionState() +50
System.Web.SessionState.SessionStateModule.BeginAcquireState(Object source, EventArgs e, AsyncCallback cb, Object extraData) +659
System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +96
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

My WebConfig include Following Statement.

    <sessionState mode="SQLServer"
      cookieless="true"
      regenerateExpiredSessionId="true"
      timeout="30"
      allowCustomSqlDatabase="true"
      sqlConnectionString="Data Source=hostingServer;Initial         catalog=MyDatabase;User Id=MyUser;password=MyPassword;"
      stateNetworkTimeout="60"/>

I have run aspnet_regsql.exe. It asked me about the server and login detail after that it created aspNret_TableNames.

Please guide me.

Upvotes: 2

Views: 11433

Answers (1)

Shrewdroid
Shrewdroid

Reputation: 800

I believe you must have used the aspnet_regsql.exe application which then starts a wizard and then adds various aspnet_* tables to your tables.

If so then again restart the same wizard and then choose the remove option to remove all those tables from the database.

Now run this command:

aspnet_regsql.exe -ssadd -d <Your Database> -sstype c -S <Server> -U <Username> -P <Password>

This will then add two tables to your database, namely ASPStateTempApplications & ASPStateTempSessions.

Modify your web.config file to include the following configuration:

<sessionState
    mode="SQLServer"
    allowCustomSqlDatabase="true"
    sqlConnectionString="Data Source=Server;Initial Catalog=Database;User ID=UserId;Password=Password"
    cookieless="false" timeout="20" />

NOTE: 1. I have assumed that you want to store session within your applications database. If you want to maintain the session database separately then run the above command without the "-d" parameter. This will create a new ASPState database with two table that I have specified above. And finally you can specify the name of this database in your configuration.

Hope this helps :)

Upvotes: 13

Related Questions