Accessing session variables within classes in App_Code

Because ASP.NET pages contain a default reference to the System.Web namespace (which contains the HttpContext class), you can reference the members of HttpContext on an .aspx page without the fully qualified class reference to HttpContext. If you try to access this property within a class in App_Code, the property will not be available to you unless your class derives from the Page Class.

In this case, you can always use a wrapper class around the ASP.NET session to simplify access to session variables.

Sample Code:

public class YourSession
{
private YourSession()
{
Property1 = “default value for property 1”;
Property2 = “default value for property 2”;
Property3 = “default value for property 3”;
}

public static YourSession Current
{
get
{
YourSession session =
(YourSession)HttpContext.Current.Session[“__YourSession__”];
if (session == null)
{
session = new YourSession();
HttpContext.Current.Session[“__YourSession__”] = session;
}
return session;
}
}

public string Property1 { get; set; }
public string Property2 { get; set; }
public string Property3 { get; set; }
}

This above class stores one instance of itself in the ASP.NET session and allows you to access your session properties in a type-safe way from any class. The session properties can be assessed in the following manner:

string property1 = YourSession.Current.Property1;
YourSession.Current.Property1 = new value for property 1″;

string property2 = YourSession.Current.Property2;
YourSession.Current.Property2 = new value for property 2″;

string property3 = YourSession.Current.Property3;
YourSession.Current.Property3 = new value for property 3″;

Advertisements

ASP.NET Website Administration Tool: Unable to connect to SQL Server database

ASP.NET Website Administration Tool: Unable to connect to SQL Server database

If you have problems connecting to SQL Server database through the ASP.NET Website Administrator Tool, you may try to perform one of the following steps:

Step 1:
In some cases, you need to change the connection string:

<connectionStrings>
<add name=”ApplicationServices”
connectionString=”data source=##SERVERNAME##;Integrated Security=True;Initial Catalog=aspnetdb”
providerName=”System.Data.SqlClient” />
</connectionStrings>

Step 2:
In some cases, the ASP.NET service account (Machine_X\ASPNET or Network Service) requries access granting to the source database:

— Grant ASPNET access to the database
USE [##DBNAME##]
GO

— Grant login to Network service for ASPNET membership
EXEC sp_grantlogin N’##SERVICEACCOUNT##’
go

— Grant minimum permissions
USE [##DBNAME##]
GO
sp_addrolemember ‘aspnet_Membership_FullAccess’, N’##SERVICEACCOUNT##’
GO

IMPORTANT NOTE:
From the information that I have gathered throughout the Internet, I believed you cannot use the AttachDbFilename with a full blown SQL Server. Either add “\SQLEXPRESS” (instance name) to your server name or get rid of the AttachDbFileName and use Database=”##DATABASENAME##”.

Default connection string for ASP.NET Web Site Administration Tool

By default, the ASP.NET Web Site Administration Tool uses connection string named “LocalSqlServer” to connect database. It is “data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true” by default.

If you need to reconfigure the connection string “LocalSqlServer”, then you can make the following changes in your Web.config file:

<connectionStrings>
<remove name=”LocalSqlServer” />
<add name=”LocalSqlServer” connectionString=”your connection string” providerName=”System.Data.SqlClient” />
</connectionStrings>

ASPStateTempSessions and the ASPStateTempApplications tables

You can configure Microsoft SQL Server for ASP.NET SQL Server mode session state management.

Why there are no tables in the ASPState database after the configuration?
When you use the default InstallSqlState.sql script file to configure ASP.NET SQL Server mode session state management, the script file adds the ASPStateTempSessions and the ASPStateTempApplications tables to the tempdb database in SQL Server by default. If you restart SQL Server, you will lose the session state data that was stored in the ASPStateTempSessions and the ASPStateTempApplications tables.

How the deletion of expired sessions work?
The InstallSqlState.sql script creates a job called ASPState_Job_DeleteExpiredSessions to delete expired sessions from tempdb. Recall that ASP.NET does not keep session resources alive indefinitely. To support this feature when a SQL Server is used to maintain state, the SQL Server Agent must be running so that the expired session deletion job runs as needed. By default, the job is scheduled to run every minute. It deletes session state rows with an Expires value less than the current time. The account under which the SQL Server Agent runs must have the privilege to execute the DeleteExpiredSessions stored procedure.