Problem making shopping cart work properly.

Drigo2012

New member
Joined
Apr 23, 2009
Messages
1
Programming Experience
1-3
Hi all (first time posting :p(Hopefully in the right place too :p))
The code Im going to put is working but not the way i want it to. Its easier to explain with pictures so here they are:
Picture1
In that picture you can see the buy button that will take you into the shopping cart showed bellow:
Picture2
The problem Im having is that if I click in another buy button that is not the 1st, it will still grab the pokemon data and put it in the shopping cart.
So here is my code behind:
-----------------------------------------------------
Imports System.Data

Partial Class NDS_NDSList
Inherits System.Web.UI.Page
Private SelectedProduct As New Product

Protected Sub btnBuy_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If Page.IsValid Then
Dim CartItem As New CartItem
CartItem.Product = SelectedProduct
CartItem.Quantity = 1 'CType(txtQuantity.Text, Integer)

Me.AddToCart(CartItem)
Response.Redirect("~/Cart.aspx")
End If
End Sub

Private Sub AddToCart(ByVal CartItem As CartItem)
Dim Cart As SortedList = GetCart()
Dim sGameID As Integer = SelectedProduct.GameID
If Cart.ContainsKey(sGameID) Then
CartItem = CType(Cart(sGameID), CartItem)
CartItem.Quantity += 1 'CType(txtQuantity.Text, Integer)
Else
Cart.Add(sGameID, CartItem)
End If

End Sub

Private Function GetCart() As SortedList
If Session("Cart") Is Nothing Then
Session.Add("Cart", New SortedList)
End If
Return CType(Session("Cart"), SortedList)
End Function

Private Function GetSelectedProduct() As Product
Dim dvProduct As DataView = CType( _
SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
'dvProduct.RowFilter = "SerialNumber = '" & FormView1.SelectedValue & "'"
Dim Product As New Product
Product.GameID = CInt(dvProduct(0)("GameID"))
Product.GameName = dvProduct(0)("GameName").ToString
Product.Price = dvProduct(0)("Price")
Product.PictureLocation = dvProduct(0)("PictureLocation").ToString
Return Product
End Function

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SelectedProduct = Me.GetSelectedProduct()

'DataList1.SelectedValue.FindControl("PriceLabel") = FormatCurrency(SelectedProduct.Price)

End Sub

'Protected Sub DataList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataList1.SelectedIndexChanged
'
'End Sub
End Class

-------------------------------------
I think the problem lies in the Private Function GetSelectedProduct:
"Product.GameID = CInt(dvProduct(0)("GameID"))"
A friend told me that the zero between the parenthesis points only to the first game in the DataList, but he didnt know how to make it work the proper way. He said it had something to do with the SelectedIndexChanged that I left commented out at the end of the code.
Can anyone help?
If you need more info(like the aspx page), plz let me know.
 
Back
Top