Advertisment

Profile Management 

author-image
PCQ Bureau
New Update

With User Profiles in ASP.NET 2.0 we can create extremely dynamic and powerful sites with built-in personalization. And all this without requiring writing any data retrieval code at all. This month we continue with profile management in ASP.NET 2.0 and look at some of the more advanced features in it.

Advertisment

We have seen that creating a profile with system datatypes is quite simple. Types such as numbers, strings and even color were easily used within the Profile object. But what about being able to use the types that you create? Let's create a new datatype that holds the Shopping Cart for a user. This cart can have multiple items within it, each having a unique ID, name, price and quantity. The code for these classes would look something like the one given below. Note that the actual code has been reduced for brevity. You will find the complete code for the Shopping Cart in either your MSDN samples or you can download the "modified for PCQ" version from the Forums. 

Imports Microsoft.VisualBasic



_


Public Class ShoppingCart


Public _CItems As New Hashtable


Public ReadOnly Property CartItems() As ICollection


Get


Return _CItems.Values


End Get


End Property


Public ReadOnly Property Total() As Decimal...


Public Sub AddItem(ByVal ID As String, ByVal Name As String, ByVal Price As Decimal)...


Public Sub RemoveItem(ByVal ID As String)...


End Class


_


Public Class Item


Private _ID As String


Private _Name As String


Private _Price As Decimal


Private _Quantity As Integer = 1


Public ReadOnly Property ID() As String...


Public ReadOnly Property Name() As String...


Public ReadOnly Property Price() As Decimal...


Public Property Quantity() As Integer...


Public Sub New(ByVal ID As String, ByVal Name As String, ByVal Price As Decimal)...


End Class






















Direct Hit!
Applies

to:
.NET developers
USP:

Better developer productivity
Links:

http://lab.msdn.microsoft.com/ vs2005/microsoft. com/vs2005/ 
Advertisment

Both the classes have been marked as Serializable so that they can be serialized across when transferring data. To create a profile property that contains the Shopping Cart, you need to add the following in your

Web.Config.













Advertisment




The new profile property called 'Cart' which is serialized as binary and allows anonymous users to use it as well, is of type ShoppingCart. This means that all the properties and methods of this type are also available to the profile automatically. To use this within a page, let's create a page that shows a list of products, where on selecting one, the cart is updated. The grid called 'gvProducts' displays the products as well as a select button and the grid called 'gvCart' is the Shopping Cart. Showing the list of items in a cart is accomplished by this procedure:

Advertisment

Sub BindCart()


If Not Profile.Cart Is Nothing Then


gvCart.DataSource = Profile.Cart.CartItems


gvCart.DataBind()


lblTotal.Text = String.Format("Total Cost in Cart: {0}", _


Profile.Cart.Total.ToString("c"))


End If


End Sub






You can add an item into the cart by selecting an item in the Products grid like this.

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



Dim row As GridViewRow = gvProducts.SelectedRow


Dim ID As String = gvProducts.SelectedDataKey.Value


Dim Name As String = row.Cells(2).Text


Dim Price As Decimal = CDec(row.Cells(3).Text)


If Profile.Cart Is Nothing Then


Profile.Cart = New ShoppingCart()


End If


Profile.Cart.AddItem(ID, Name, Price)


BindCart()


End Sub








Advertisment

Executing this will let you use the Shopping Cart to add items. 

In its present form, the above code will allow users to add items to their carts. If a user is anonymous he has a cart that is linked with a randomly generated UserName. But, if the user is authenticated, he has a cart linked with his real username. This means that if a user is anonymous and creates a list of items in his shopping cart, the moment he logs in, he will lose all those items and start with a new cart. To prevent this from happening you can migrate the anonymous user's profile information to the authenticated user by creating a simple event in the site's Global.asax file like the following:

Sub Profile_MigrateAnonymous(ByVal s As Object, ByVal e As ProfileMigrateEventArgs)



Dim anonProfile As HttpProfile = Profile.GetProfile(e.AnonymousId)


Profile.Cart = anonProfile.Cart


End Sub

Advertisment

To test this out, you can add a login and logout button on the products/cart page you created above to simulate a forms login. Remember that you also need to set the site to use forms authentication in the Web.config file. Once done, you can move in as an anonymous user, build a cart and then login. You'll see the cart being maintained perfectly without getting lost-even across sessions. Comment out the code above in the asax file and then see the difference.

By default, ASP.NET uses an Access database file in the website's Data folder to store profile information. But, you are free to change it to any other provider. We'll change it to SQL Server. First run a program that creates the database and/or the table required for storing all the profile information. Run the ASPNET_ REGSQL.EXE in the Framework \V2.0.x folder of the .NET framework. The wizard that comes up will guide you through the setup. Now configure the site to use this new provider, using the website's configuration manager. Create a new provider of SQL Server type and point it to the correct server, database and with the required permissions. This will make the appropriate changes required in the Web.config file. You can now delete the mdb file in the website and still continue to run the application-now storing the profile data in the SQL Server database you specified. 

You can download the complete working code and solution from the PCQuest forums' developer section. 

Vinod Unny Enterprise InfoTech

Advertisment