1. Computing & Technology

Discuss in my forum

Maintaining State With Profiles in ASP.NET Web Pages

Exclusively for 'by user' state information.

By , About.com Guide

Updated October 18, 2010

Hopefully, you reached this page from the About Visual Basic ASP.NET Hub Page. If not, you might want to click the link above and go there.

The entire web is inherently "stateless" because it's a fundamental part of the http protocol which controls the way the entire web works. Click here for an overview of state in ASP.NET. This article introduces ASP.NET profiles and shows how they can be used to maintain state.

Most of the objects that are used to maintain state in an ASP.NET application are designed for that purpose. The usual techniques include view state, session state, and application state.

Profiles are different in several ways. They're designed to save individualized information about a user that is normally authenticated (logged in) to your web application. In my recent book on ASP.NET, we placed the chapter on profiles in the security section of the book, not the one on state management. But since profiles do persist information for a web site, they're clearly another way of maintaining state.

In addition to maintaining information for a specific user, profiles are also different because they require permanent storage of the information. Most profiles are stored in a database and ASP.NET gives you a way to do that without making you set one up yourself. But because ASP.NET handles the whole thing for you (unless you create your own profile provider - an advanced topic), the entire block of profile information is made available for your application when it's first accessed and then the entire block is written back when page processing is complete. You can't request just some of it. For this reason, profiles become inefficient if you're trying to manage a large amount of information. If you have a requirement like that, you're probably better off setting up your own database. Microsoft also uses a proprietary format for profile information so the profile data store can't be used offline like a regular database could.

Finally, since profiles work best if the user of the application is an authenticated user - Windows authentication, forms authentication, or custom authentication - there are extra steps necessary for anonymous users and even more steps if that anonymous user accesses profile information and then logs into the application.

Here's how to add profiles to your application using standard Windows authentication. If you're just using Windows on your own PC, this should work for you. (The more advanced profile providers aren't covered here.)

Step 1. Make sure you have SQL Server Express installed.

The free SQL Server Express database makes this step easier. If you have SQL Server Express installed, the first time you use a profile, ASP.NET will create a new database named aspnetdb.mdf in the App_Data subdirectory of your application and add the profiles tables to the database for you. It will even add the tables to an existing aspnetdb.mdf database if you're already using one for a different feature.

Detour: I won't try to cover the details here, but if you're not using SQL Server Express, then you have to run a utility called aspnet_regsql.exe to create the aspnetdb database. You can find this utility in the Windows\Microsoft.NET\Framework\[Version] folder. This part of the job can be a little tricky because aspnet_regsql.exe is a command line utility. (It runs in the Command Prompt window.) The MSDN documentation, or my ASP.NET book has details about how this works, but this command will create the aspnetdb database with the default name on the current computer by logging into the database using the current Windows account:


aspnet_regsql.exe -A p -E

The aspnetdb database contains a number of tables including a one-to-one relationship between profiles and users and a many-to-one relationship between users and applications. Once you establish the database, you can view the tables with the Server Explorer window in Visual Studio as shown below. In this case, a bunch of test users that I created while working on the ASP.NET book are shown.

--------
Click Here to display the illustration
--------

Step 2. Add the variables to web.config.

To get back on track, if you only want to use profiles to save information, VB will do most of the work. You do, however, have to add the variables to the web.config file for your ASP.NET application. Do that like this (other web.config elements not shown for simplicity):


<?xml version="1.0"?>
<configuration>
  <system.web>
    <profile>
      <properties>
        <add name="FirstName" type="String"/>
        <add name="LastName" type="String"/>
        <add name="DateOfBirth" type="DateTime"/>
      </properties>
    </profile>
  </system.web>
</configuration>

You can then use the profile variable with this syntax:


Profile.FirstName = "George"

You get this error when you try to use a Whatever profile variable that hasn't been added to web.config:


'Whatever' is not a member of 'ProfileCommon'.

In the previous example, ASP.NET used an existing aspnetdb database that was available to my local instance of SQL Server. Given my configuration, to save the profile information in a database added to the App_Data folder in my own project, I had to add more code to web.config.


<?xml version="1.0"?>
<configuration>
  <system.web>
    <profile defaultProvider="SqlProvider">
      <providers>
        <clear />
        <add name="SqlProvider"
        type="System.Web.Profile.SqlProfileProvider"
        connectionStringName="SqlServices" />
      </providers>
      <properties>
        <add name="FirstName" type="String"/>
        <add name="LastName" type="String"/>
        <add name="DateOfBirth" type="DateTime"/>
      </properties>
    </profile>
  </system.web>
  <connectionStrings>
    <add name="SqlServices" connectionString=
    "data source=.\SQLEXPRESS;Integrated Security=SSPI;
    AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" />
  </connectionStrings>
</configuration>

For what they do best ... permanently persisting critical user information ... profiles can't be beat. But if all you want to do is save state information, you're probably better off using something else and avoiding the extra work.

©2012 About.com. All rights reserved.

A part of The New York Times Company.