Advertisment

Manage User Information 

author-image
PCQ Bureau
New Update

Most websites, intranets and portals ask users for information, such as contact information or preferences (like favorite color, theme and categories). Using this information the site delivers the content, most suitable to a particular user. For instance, a user can set up the home page that displays a particular theme, with only the categories and items of his interest. And so whenever he visits that page, his preferences are recalled and even a personalized welcome note 'greets' him.

Advertisment

For this, a good amount of coding goes into asking the user for information, creating a database connection and then storing it into the database. Whenever required, you can query the database and the appropriate information will be retrieved and displayed. However, there is a way out. The ASP.NET 2.0 introduces a feature called Profiles that lets you quickly configure, store and retrieve this information without keying in the lengthy code.

To set up profiles in your Web application, you first need to create a Web.config file for that application. In our example below, we've created a small profile for storing the information about a user visiting our page.

Direct Hit!
Applies

to:
.NET developers
USP:

Create profiles without lengthy coding
Links:

msdn.microsoft.com/asp.net/whidbey/default.aspx  
Advertisment



















































Advertisment





This Web.config file creates a new profile having the properties: Name (string), Age (Single), PreferredColor (Color) and a group of properties called ContactInfo (all of type string) that comprise the Address, City and PIN. 

One important thing to note is that you can define any valid .NET type as the property's type. For instance, the PreferredColor property is of the Color type. However as this is a complex type, we need to serialize this as binary, rather than the default XML serialization. 

Once this is done, the profile is ready for use. To store the profile of a user coming to this site, let's create a simple Web form

that can capture the information to the profile.

<%@ Page Language="VB" AutoEventWireup="false" CompileWith="Default.aspx.vb" ClassName="Default_aspx" %>





Untitled Page













Your Name:




Your Age:  




Address:







City:



PIN:



Your preferred Color:


Blue


Green


Red


Yellow







 








Go To Page 2


















The above page creates a simple Web form that will ask the user for the different items we need in his profile. To store the profile, we will create the Button1_Click event for the Save button as below.

Advertisment

Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)



Profile.Name = TextBox1.Text


Profile.Age = TextBox2.Text


Profile.ContactInfo.Address = TextBox3.Text


Profile.ContactInfo.City = TextBox4.Text


Profile.ContactInfo.PIN = TextBox5.Text


Profile.PreferredColor = System.Drawing.Color.FromName(DropDownList1.SelectedValue)


End Sub





As can be seen in the code, the profile object exposes the properties you set in the Web.config. The ContactInfo group shows up as a nested object that exposes properties of its own by using it like Profile.ContactInfo.City. In fact, if you are writing this code in VS.NET 2005, the Intellisense automatically lets you pick properties and groups easily. 

Once the values are set in the profile object, they are retained across user sessions as well. Let's create another page, Default2.aspx that looks like this:

Advertisment

<%@ Page Language="VB" AutoEventWireup="false" CompileWith="Default2.aspx.vb" ClassName="Default2_aspx" %>






Untitled Page







Hi ,



You contact info is:


























Your Age is:  
Bold="True">










Advertisment

And on its page load, we have an event handler, which looks like this:

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load



Label1.Text = Profile.Name


Label2.Text = Profile.ContactInfo.Address


Label3.Text = Profile.ContactInfo.City


Label4.Text = Profile.ContactInfo.PIN


Label5.Text = Profile.Age


Calendar1.BackColor = Profile.PreferredColor


End Sub





When you run the first page, enter some values in the form and save it, the profile gets stored. The second page retrieves and displays the profile. The best part is that unlike a Session object, the Profile object is persistent across browser sessions. 



This means that if the user closes the browser window and comes back to the second page again after a while, the page will remember his profile settings and display them correctly. It also works across for different anonymous users as well, in storing their individual preferences. 

As we have seen, creating and using user profiles is quite an easy task, using the new profile object model in ASP.NET 2.0. However, this is not all you can do with this object and next month, we'll continue our journey to perform advanced tasks with it. 

Vinod Unny, Enterprise InfoTech

Advertisment