 |
| ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 9th, 2007, 02:08 PM
|
|
Friend of Wrox
|
|
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
how to use shoppingcart without profile?
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("PriceLab el"), 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("PriceLab el"), Label).Text)
...
Dim newcart As New ShoppingCart()
newcart.Insert(ProductID, Price, 1, ProductName, PictureURL)
End Sub
|
|

January 9th, 2007, 02:13 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

January 9th, 2007, 04:57 PM
|
|
Friend of Wrox
|
|
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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("PriceLab el"), 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
|
|

January 9th, 2007, 05:07 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

January 10th, 2007, 10:33 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

January 10th, 2007, 04:54 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

January 11th, 2007, 05:16 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes, you' re right.
|
|
 |