Where are the ASP.Net’s Profile, ProfileCommon when you need them?

We all know about the ability to maintain a profile for an user of an ASP.Net Web Site. I am going to assume that you already configured the aspnetdb database on your SQL server of choice in case you decided not to go with the disconnected aspnetdb in the App_Data folder.

Define a custom Profile in web.config

In many books they tell you to edit web.config by adding the <profile> element and within the <profile> element add a <properties> element which will finally contain the properties of the profile. It looks like this:
<authentication mode=”Forms”>
<forms defaultUrl=”Login.aspx” ></forms>
<connectionStrings >
<add name=”LocalSql2008Express” connectionString=”Data Source=HYPERCUBE\SQLEXPRESS;Initial Catalog=aspnetdb;Integrated Security=True”/>
<profile enabled=”true” defaultProvider=”SqlProfileProvider”>
<add name=”SqlProfileProvider” type=”System.Web.Profile.SqlProfileProvider” connectionStringName=”LocalSql2008Express”
<add name=”PreferredLanguage” allowAnonymous=”true” type=”System.String”/>
Now this book I was reading tells me that I should be able to access the PreferredLanguage property from the code behind this way: Profile.PreferredLanguage, where Profile is an instance of ProfileCommon, a class created behind the scenes and which exposes all the property definitions from the web.config file as property members of the ProfileCommon class (and obviously Profile too as it is an instance of ProfileCommon).
What the book that I read did not tell me is that in Visual Studio 2005/2008 this luxury of having the Profile instance created automatically for you is available only if your web site is a WebSite Project and it doesn’t work if you use a WebApplication Project. More, from what I see in Visual Studio 2010,  it doesn’t work at all with either type of web project. I am pretty sure of that, as I have brought a Web Site project up and Profile and ProfileCommon are not recognized by the compiler.
Actually after I installed the RTM I can say that VS 2010 does actually create Profile instance of a ProfileCommon, automatically generated class.
However, while racking my brains to understand what is going on I came across a post in the Visual Studio 2008 documentation which helps one in coding a ProfileCommon class and then instantiate a Profile variable of type ProfileCommon in the code behind class of certain pages as needed.
The Visual Studio 2008 Web application project does not automatically include the ProfileCommon class. However, you can create your own ProfileCommon class that contains strongly typed properties for the items configured in the profile system. Then you can access the current Profile property of the HttpContext object to get and set the properties. The following example shows how to create a custom ProfileCommon class.
So, you can spend some effort and code the ProfileCommon yourself. Let’s see how the code should look like:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Profile;

namespace ProfileTest
public class ProfileCommon
public string PreferredLanguage
get{ return (string) HttpContext.Current.Profile.GetPropertyValue(“PreferredLanguage”); }

HttpContext.Current.Profile.SetPropertyValue(“PreferredLanguage”,value); }
You can then add an instance of the ProfileCommon class named Profile to the pages that must use the profile system, as shown in the following example.

public partial class _Default : System.Web.UI.Page
ProfileCommon Profile = new ProfileCommon()
txtLanguage.Text = Profile.PreferredLanguage;

Define a custom Profile in code

It is an inconvenience not having the Profile object created automatically for you so you can easily access the profile’s properties. There is another approach to this and that is to not use web.config at all to define the profile but rather do it in code. Yes, create a custom profile by inheriting the ProfileBase. I am not going to describe the whole process here, as there is an excellent blog entry by Jon Galloway at
May 1st, 2010