Understanding the Profile Object of ASP.NET Version 2.0 (Beta 1)
page 3 of 4
by Terry Voss
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 23062/ 80

Basic Example

[Download Sample Project]

Let's create a ShoppingCart class and use it in Profile.  First, we create an ItemOrder class with a simple version of an order, then a collection container for it, and then the code using it.  Note that if you just declare two public fields for ProductID and Quantity and not method properties, your collection of ItemOrder classes will not automatically bind to the GridView or DataGrid. Also note the two constructors or constructor overload as they come into importance later.

ItemOrder Class
<Serializable()> Public Class ItemOrder

Sub New()
End Sub

Sub New(ByVal qty As Integer, ByVal product As Integer)
  _quantity = qty
  _productID = product
End Sub

Private _quantity As Integer
Private _productID As Integer

Public Property Quantity() As Integer
  Get
    Return _quantity
  End Get
  Set(ByVal Value As Integer)
    _quantity = Value
  End Set
End Property

Public Property ProductID() As Integer
  Get
    Return _productID
  End Get
  Set(ByVal Value As Integer)
    _productID = Value
  End Set
End Property

End Class

Shopping Cart Class
Imports System.Collections.generic

<Serializable()>Public Class ShoppingCart : Inherits List(Of OrderItem)
End Class

Code-Beside Class
Imports System.Web.Profile

Partial Class viewprofile_aspx

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  If Not IsPostBack Then
    Profile.Cart = New ShoppingCart
  End If
End Sub

Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  Dim item As New ItemOrder(quantity.Text, product.Text)
  Profile.Cart.Add(item)
  GridView1.DataSource = Profile.Cart
  GridView1.DataBind()
End Sub

Web.config Section
<profile defaultProvider="Developer2 Data">
  <properties>
    <add name="Cart" type="MyNamespace.ShoppingCart" serializeAs="Binary" allowAnonymous="true" />
  </properties>
</profile>
 

Note the little code for ShoppingCart; because it inherits from a generic List dedicated to only ItemOrder objects, all we need is the inherited Add, Remove, Count, etc. methods for a cart.  Note then that only two lines of code add an item to the cart with the button click.  Two lines bind the result to the GridView1 control. 

Both custom objects must have the attribute for Serializable affixed since this information must be serialized by the Profile object. There are two methods of serialization with the default being XML serialization. I have selected binary serialization in the above web.config entry at the suggestion of Dino Esposito in his book linked below (page 111). He was using (it seems) pre-Beta 1 code, and I had no trouble with collections and Profile with either serialization type, except for automatic referencing.

In Beta 1, if you create your custom classes in the code folder it is a dynamically compiled file, but the dynamically generated assembly is not automatically referenced. Therefore, currently in Beta 1, you will get Intellisense for your new cart after putting it into the website menu's ASP.NET configuration option's Create Profile property area, but you will get a reference error. Currently you will have to manually compile your own classes to a DLL that you place into the bin folder under your application's bin directory. I placed my two .vb files into the bin folder and used the following compile statement in a batch.bat file:

 
cd "c:\websites\profile\bin\"
C:\WINDOWS\Microsoft.NET\Framework\v2.0.40607\vbc
/out:ShoppingCart.dll /target:library /optimize /recurse:*.vb 
/r:system.dll,system.data.dll,system.enterpriseservices.dll,system.xml.dll

The shopping cart then works fine, showing the added items in the GridView1 control piling up. When I took the isSerializeAs="Binary" out, so that the default serialization would be used, I got an error indicating that ItemOrder class did not have an empty default constructor. I added the binary serialization option back, and things worked again.

The other constructor is added so that I can create an ItemOrder instance with one line of code. I am hoping very much that, by Beta 2, the custom type referencing will be automatic (I have recently confirmed with Microsoft that these problems with the custom data type will be fixed by beta2). If you note the one line in the Page load event, I wish the Profile property allowed me to check that I want a new instance of my type put into the Profile custom property, so I don't have to do that line like the simple types do.


View Entire Article

User Comments

Title: The shopping cart works great, but...   
Name: scott baldridge
Date: 2006-02-21 10:58:14 PM
Comment:
I have a profile.shoppingCart working but am stuck with finding information on how to get the items from the cart to a SQL database. I can find no documentation or examples. Does anyone know how?
It's all easy to put the data into the cart but is useless if you can't get it to a database..

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-01-16 7:31:32 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search