Shopping Cart ClassImports System.Collections.generic
<Serializable()>Public Class ShoppingCart : Inherits List(Of OrderItem)
End Class
Code-Beside ClassImports 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.