Working with Asp.Net profile properties to save user specific information

Hi,

Many a times we need to keep unique information of the user in the web application so that the information can be used for the user when required. For example, you can allow the user to choose the background image of the site. Or even let the user select a theme from the many themes in the site.
[Note: The multiple theme property has already been incorporated in my site. You can choose the Change The Theme button in the left top to change the theme of this site.]

We normally want to store some values for individual user, so as to allow personalization of the site for individual user. These profile values need to be stored in database so that they can be retrieved and used when the user visits again.

Asp.Net profile stores the information of the individual user in persistent format. The bets thing about Asp.net profile is that it allows us to manage user information without having to maintain a database of our own.

Another better feature of Asp.Net profile is that it allows us to access the profile property of individual user using a strongly typed API that is accessible through out the application.

We can store nearly anything and everything in the profile. The profile feature provides a very generic storage feature that let us define and maintain almost all type of data.

To use Asp.Net profile we need to first enable the profile in out application. This can be done through web.config. You need to provide the provider for the profile, which does the job of doing the low level work of storing and retrieving the data of profile. We can very well use the provider provided by the framework or create our own provider. By default the data is stored in SQL Server (express edition in App_data folder).

We need to define the list of properties that we want to maintain in the profile section of the configuration. In my case I used this to store the selected theme of the site. We can also assign the default value of the property with the help of the attribute defaultValue and define if the property will be stored for anonymous user with the help of the attribute allowAnonymous. Here is an example of how to set a profile property.

<profile>

     <properties>

<add name="ThemeName" defaultValue="Vikram1" allowAnonymous="true"/>

     </properties>

</profile>

Here I have added a property ThemeName to be stored in the profile. This property will be available to me in the application. Now I can get or set the value of the property in the application, whenever the user changes the default theme for themselves.

When our application runs, ASP.NET creates a ProfileCommon class. This class is dynamically generated class. The class inherits from the ProfileBase class. The dynamic ProfileCommon class includes properties created from the profile property definitions you specify in your application configuration.

An instance of this dynamic ProfileCommon class is then set as the value of the Profile property of the current HttpContext and is available to pages in your application.

To access the theme property in the code, we can write the following line of code.

Profile.ThemeName = ThemeName.SelectedValue;

Or

ThemeName.Selectedvalue = Profile.ThemeName;

When we set the value the value is automatically stored in the database and used next time user comes to the site. Remember you do not have to write explicit code to look up in th database for the value of these fields. Asp.net Profile provider specified does the low level work of saving and retrieving the data from the database.

Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2009 Vikram Lakhotia