Storing and Using Dynamic Profile Data


Profiles provide a very useful way to store data related to each user, including anonymous users. Previously, in earlier versions of ASP.NET or in other Web site programming languages, you would have to create and manage a database to persist values that you can store in the profiles systems in ASP.NET 2.0. The profiles system provides a full set of features for managing the data it holds, including tasks such as migrating values from an anonymous profile to a known user's profile when that user logs in to the site, and automatically serializing and deserializing the stored data from complex data structures, such as a shopping cart.

The profiles system exposes all its features through static properties and methods of the Profile class that ASP.NET generates automatically at runtime for your application, based on the settings in your web.config files. Accessing a user's profile information is as simple as reading and writing the values of properties of the Profile object.

Profiles have several advantages over other built-in persistence features such as Session state. Because the values are stored in a database, they persist beyond the end of a user's session. Therefore, they are ideal for storing long-term data such as a visitor's name and address, visual site customization preferences, and other information that must survive over long periods.

Another major advantage of the profiles system is that the values it stores are typed (including complex user-defined types), rather than just being String values (as you would store in the ASP.NET Session, Application, or ViewState). This means that there is no need to cast, convert, or parse values to the appropriate type at runtime when reading them, or to cast/convert them to a String when writing them. Moreover, you are working with strongly typed instances when you access the Profile object, and so you get Intellisense and statement completion when writing code in Visual Studio and Visual Web Developer.

Reading Profile Data

All access to profile data is through the static Profile class. Therefore, given the properties defined in the web.config file shown in Listing 12.5 and Listing 12.7, you could read the stored profile data using code like that in Listing 12.9. Notice that the code checks to see if the current user is logged in or not, by accessing the IsAuthenticated property of the current User.Identity instance. This is required, because only the TextSize, SiteName, and Cart profile property declarations contain the allowAnonymous="true" attribute. Trying to access those that do not contain this attribute for an anonymous visitor raises an exception at runtime.

The code creates the output string containing the user's profile data in a StringBuilder and displays the contents in a Label control on the page. Notice how the code accesses the properties in the "Address" profile properties group using the group name. Finally, the code accesses the cart contents (properties) for the Cart profile property (the profile system will create a new empty cart automatically for each visitor), and binds the DataTable returned from the CartItems property to a GridView control to display the items in the cart.

Storing (Writing) Profile Data

Writing profile properties is just as easy as reading them. The properties are strongly typed, and so there is no need to cast or convert the values to String instances first. However, bear in mind that you cannot (obviously) write to read-only properties, and that you cannot write to properties that are not declared as allowAnonymous="true" if the user is not logged in.

Listing 12.9. Accessing Stored Profile Data

StringBuilder builder = new StringBuilder(); if (User.Identity.IsAuthenticated) {   // read normal profile properties   builder.Append(String.Format("Name: {0}, {1}",                  Profile.FirstName, Profile.LastName));   builder.Append(String.Format("Age: {0}", Profile.Age));   // read profile properties from Address profile group   builder.Append(String.Format("Address: {0}, {1}, {2}, {3}",                  Profile.Address.Street, Profile.Address.City,                  Profile.Address.ZipCode, Profile.Address.Country)); } // read anonymous profile properties builder.Append(String.Format("TextSize: {0}", Profile.TextSize)); builder.Append(String.Format("SiteName: {0}", Profile.SiteName)); // read property values from shopping cart builder.Append(String.Format("ShoppingCart: UserID: {0} "                + "TotalValue: {1}", Profile.Cart.UserID,                  Profile.Cart.TotalValue)); // display DataTable of cart items in GridView control gridCartItems.DataSource = Profile.Cart.CartItems; gridCartItems.DataBind() // display string of other property values in Label control lblProfileData.Text = builder.ToString();

Listing 12.10 shows an example of updating the profile properties declared in Listing 12.5 and Listing 12.7. You can see that the code checks the IsAuthenticated property of the current User.Identity instance before writing to the non-anonymous properties. Again, the code accesses the profile group properties using the group name "Address".

The code then (somewhat unrealistically in a "real-world" scenario) empties this visitor's shopping cart before adding some items to it.

Listing 12.10. Updating Stored Profile Data

if (User.Identity.IsAuthenticated) {   // update the normal profile properties   Profile.FirstName = "Alex";   Profile.LastName = "Homer";   Profile.Age = 21;   // update the profile properties in the Address group   Profile.Address.Street = "123 High Road";   Profile.Address.City = "UpperLittleVille";   Profile.Address.ZipCode = "123456-789";   Profile.Address.Country = "England"; } // update the anonymous profile property Profile.TextSize = "small"; // update the shopping cart, empty it first Profile.Cart.Clear(); Profile.Cart.UserID = Profile.UserName; Profile.Cart.AddItem("Small Green Thing", 1, 25.75); Profile.Cart.AddItem("Excitingly Large and Loud Thing", 1, 45.00); Profile.Cart.AddItem("Tiny But Interesting Flat Thing", 2, 3.95);

The example in the next section of this chapter combines the profile features you have seen so far to allow users to add items to a new or an existing cart, and to save the cart between visits to the site.



ASP. NET 2.0 Illustrated
ASP.NET 2.0 Illustrated
ISBN: 0321418344
EAN: 2147483647
Year: 2006
Pages: 147

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net