Subject: how to use shoppingcart without profile?
Posted By: hertendreef Post Date: 1/9/2007 1:08:03 PM
Hi,

I'm studying chapter 13 of the book "beginning asp.net 2.0".
That application makes a shoppingcart which is filled with cartitems. The shoppingcart is then put in the profile (defined in web.config) of the user.

What i try to do is not using the profile. My question is then: how to check whether the shoppingcart exist or not and how to add some items in the shoppingcart? I tried something below. I have no error but the cart stays empty.

Thanks

The code with profile is here (clicking on a button):
----------------------------------------------------
Sub btnAddToCart_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)

Dim Price As Double = Double.Parse(CType(FormView1.FindControl("PriceLabel"), Label).Text)
Dim ProductName As String = CType(FormView1.FindControl("NameLabel"), Label).Text
...
If User.Identity.IsAuthenticated Then
' create the cart if it doesn't already exist
If Profile.Cart Is Nothing Then
Profile.Cart = New wrox.Commerce.ShoppingCart()
End If
' insert the item
' if the item already exists in the cart, 1 will be added to the quantity
Profile.Cart.Insert(ProductID, Price, 1, ProductName, PictureURL)
end If
End Sub


My code:
--------
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Price As Double = Double.Parse(CType(FormView1.FindControl("PriceLabel"), Label).Text)
...
Dim newcart As New ShoppingCart()
newcart.Insert(ProductID, Price, 1, ProductName, PictureURL)
End Sub



Reply By: Imar Reply Date: 1/9/2007 1:13:09 PM
Hi there,

In your code example, the ShoppingCart goes out of scope as soon as Button1_Click is finished.

You need some form of a backing store to keep the cart on the server between browser requests. Since the web is stateless, your server for gets about you, your requests and your shopping cart as soon as the page is done rendering. Common ways to store state between browser requests are Sessions and now the new Profile. The main benefit of the Profile is that it not only survives between browser requests, but also between browser sessions.

Why don't you want to use the Profile?

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply By: hertendreef Reply Date: 1/9/2007 3:57:44 PM
Hi Imar,

Thanks for replying.

I don't want not to use profile (i'm aware of the advantages), but i'm learning and so i was wondering how and where to keep the cartitems between browser requests elsewhere than in profiles.

I did this in the global.asax:
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        Dim newcart As New ShoppingCart()
        Session.Add("newcart", newcart)
    End Sub

But then, i'm again in trouble with the code below.
I realize that i create on each browser request a new shoppingcart and that's my first problem: how to avoid recreating that? But i thought the 'insert' line would insert the data into the cart.

The second problem (populate the shoppingcart) doen't work either.

Can you, if you don't mind, give me more precise hints to do that?
Thanks in advance
Chris

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Price As Double = Double.Parse(CType(FormView1.FindControl("PriceLabel"), Label).Text)
Dim pd As Integer
Dim nwc As ShoppingCart
nwc = Session("newcart")
pd = nwc.Items(0).Quantity
'If pd = 0 Then
'newcart = New ShoppingCart
'End If
Dim pdn As String
nwc.Insert(ProductID, Price, 1, ProductName, PictureURL)
pdn = nwc.Items(0).ProductName
End Sub

Reply By: Imar Reply Date: 1/9/2007 4:07:46 PM
Yeah, storing it in session state works as well. However this:

Session("newcart")

returns an object of type Object, not ShoppingCart, so it doesn't know anything about the Insert method. You need to cast the object to a ShoppingCart first:

Dim nwc As ShoppingCart = CType(Session("newcart"), ShoppingCart)

' Do with the cart what you need to do, like call Insert:
nwc.Insert (bla bla bla)

' And store the cart in session state again:
Session("nwc") = nwc

If you don't store the changes back in Session state, all you did was change the local nwc variable, not the one in session state. However, with the last line of my code example, you replace the previous ShoppingCart with the new one, so it's ready for use in other pages.

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply By: hertendreef Reply Date: 1/10/2007 9:33:22 AM
Imar, I'm very close now, but still one problem with binding the gridview (for viewing) to the cart... I tried two ways.

this is the global.asax (i think it's ok):
-----------------------
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
Dim newcart As New ShoppingCart()
Session.Add("newcart", newcart)
End Sub

this is the button code: (i think it's ok)
-----------------------
...
Dim nwc As ShoppingCart = CType(Session("newcart"), ShoppingCart)
nwc.Insert(ProductID, Price, 1, ProductName, PictureURL)
Session("newcart") = nwc

this is code for binding the gridview (cartgrid) to the cart:
------------------------------------------------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
BindGrid()
End If

Private Sub BindGrid()
CartGrid.DataSource = CType(Session("newcart"), ShoppingCart)
' or this way: CartGrid.DataSource = Session("newcart")
DataBind()
End Sub

The first way gives error:"Unable to cast object of type 'mai.eCommerce.ShoppingCart' to type 'ShoppingCart'

The second way gives: "Data source is an invalid type.  It must be either an IListSource, IEnumerable, or IDataSource"

Thanks

Reply By: Imar Reply Date: 1/10/2007 3:54:23 PM
Sounds like there's a mismatch in class names. Do you have another object called ShoppingCart, like a page with that name and class?

This error:

Unable to cast object of type 'mai.eCommerce.ShoppingCart' to type 'ShoppingCart'

seems to suggest that the session state holds a ShoppingCart object from a different namespace....

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply By: hertendreef Reply Date: 1/11/2007 4:16:37 AM
Yes, you' re right.


Go to topic 54636

Return to index page 67
Return to index page 66
Return to index page 65
Return to index page 64
Return to index page 63
Return to index page 62
Return to index page 61
Return to index page 60
Return to index page 59
Return to index page 58