Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion 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
 
Old March 8th, 2011, 07:16 PM
Authorized User
 
Join Date: Dec 2010
Posts: 21
Thanks: 4
Thanked 1 Time in 1 Post
Question Data Table Session for VB shopping Cart

So I have put together a shopping cart program, and it took me almost a month to figure all of this out. I still only halfway understand it, but as long ast it works, I'm happy

Now I've run into another speed bump... the third one this month. It usually takes me a week to get back on my feet, but I've run out of sources. The following code is what I have for my shopping cart:

Code:
<script runat="server">

'---------------------------------------------------------------------
'	Sessions Saved Are:
'
'	Total order cost = CType(Session.Item("Total"), Decimal)
'	Item Quantity = CType(Session.Item("Quantity"), Integer)
'	Item Name = CType(Session.Item("Name"), String)
'	Cost of one unit = CType(Session.Item("Cost"), Decimal)
'
'	
'---------------------------------------------------------------------

Dim objDT As System.Data.DataTable  
Dim objDR As System.Data.DataRow

Private Sub Page_Load(s As Object, e As EventArgs)
	If Not IsPostBack Then
		makeCart()
    End If
    
End Sub

Function makeCart()
    objDT = New System.Data.DataTable("Cart") 
    objDT.Columns.Add("ID", GetType(Integer)) 
    objDT.Columns("ID").AutoIncrement = True 
    objDT.Columns("ID").AutoIncrementSeed = 1 

    objDT.Columns.Add("Quantity", GetType(Integer)) 
    objDT.Columns.Add("Product", GetType(String)) 
    objDT.Columns.Add("Cost", GetType(Decimal)) 
 
    Session("Cart") = objDT 
End Function 

Sub AddToCart(s As Object, e As EventArgs)  
 
    objDT = Session("Cart")
    Dim Product = CType(Session.Item("Name"), String)
 
    objDR = objDT.NewRow
    objDR("Quantity") = CType(Session.Item("Quantity"), Integer)
    objDR("Product") = CType(Session.Item("Name"), String)
    objDR("Cost") = CType(Session.Item("Cost"), Decimal)
    objDT.Rows.Add(objDR)
    Session("Cart") = objDT
    
    dg.DataSource = objDT
    dg.DataBind()

	Session("Cart") = objDT

End Sub

</script>
The gist of it is that it creates the columns when the page initially loads, then when the subroutine "AddToCart" is called, a row with the item information appears. Notice that I called a session for each column in the row. The sessions are saved when you click an "Add To Cart" button on another page:

Code:
Sub AddItem(sender As Object, e As System.EventArgs)

Session("Total") = Totallbl.Text
Session("Quantity") = AlmondBarkQty.Text
Session("Name") = AlmondBarkddl.SelectedItem.Text
Session("Cost") = CaseCostlbl.Text

Response.Redirect("ThePageWithTheShoppingCart.aspx")

End Sub
My problem is that when you navigate away from the shopping cart page, the data isn't saved. I have tried to make the following changes, but all this has done is caused all sorts of additional problems:

Code:
Private Sub Page_Load(s As Object, e As EventArgs)
	DataForGrid()   '<--NEW LINE'
	If Not DataForGrid() Then     '<--NEW LINE'
		If Not IsPostBack Then
		makeCart()
		End If
    End If
End Sub

Function DataforGrid()

	Dim objDT As System.Data.DataTable = CType(Session.Item("Cart"), System.Data.DataTable)

     dg.DataSource = objDT
     dg.DataBind()
    
     Session("Cart") = objDT

End Function
Problems:
1) when you navigate away from the page and come back the cart is still there, but if you leave and come back a second time, it's gone

2) when you add one item to the cart, and try adding a different item, the first item disappears, and another table is made.

Remember, I'm still new to this stuff. It's probably a really simple solution, but I've been busting my head over this so much that my brain is pretty much fried. I would really appreciate any additional help for this issue.
 
Old March 9th, 2011, 06:35 AM
Registered User
 
Join Date: Mar 2011
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Default RE: Data Table Session for VB shopping Cart

Hi MtheK,

You need to do bunch of changes in your Cart page code (where you are showing added items).

makeCart method needs to change to check whether the session object already has Cart DataTable object. With your current logic, when you visit or revisit cart page previous items will disappear.

change your makeCart method as below

Function makeCart()
if Session("Cart") is Nothing Then
objDT = New System.Data.DataTable("Cart")
objDT.Columns.Add("ID", GetType(Integer))
objDT.Columns("ID").AutoIncrement = True
objDT.Columns("ID").AutoIncrementSeed = 1

objDT.Columns.Add("Quantity", GetType(Integer))
objDT.Columns.Add("Product", GetType(String))
objDT.Columns.Add("Cost", GetType(Decimal))

Session("Cart") = objDT
End if
End Function



Private Sub Page_Load(s As Object, e As EventArgs)
If Not IsPostBack Then
makeCart()
End If
DataForGrid() '<--NEW LIN

End Sub

Function DataforGrid()

Dim objDT As System.Data.DataTable = CType(Session.Item("Cart"), System.Data.DataTable)

dg.DataSource = objDT
dg.DataBind()

End Function

Hope it helps.
Regds,
Basavaraj
The Following User Says Thank You to basavarajsg For This Useful Post:
MtheK (March 9th, 2011)
 
Old March 9th, 2011, 08:28 PM
Authorized User
 
Join Date: Dec 2010
Posts: 21
Thanks: 4
Thanked 1 Time in 1 Post
Default

Thanks a lot! That worked great! I've been busting my head over this for a month, and you got back to me and fixed it in like a day!


One more thing (of course perhaps I should move this to another thread)

I am attempting to add a delete command to my data grid, and it worked fine on my previous program, but now for some reason is giving me this error message:

Quote:
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
The data grid is in this format:
HTML Code:
<asp:DataGrid id=dg runat="server"  ondeletecommand="Delete_Item">  
<columns>
<asp:buttoncolumn buttontype="PushButton"   
commandname="Delete" text="Remove All" />  
</columns>  
</asp:DataGrid>
and the "Delete_Item" subroutine is here:
Code:
Sub Delete_Item(s As Object, e As DataGridCommandEventArgs)  
 
    objDT = Session("Cart")  
    objDT.Rows(e.Item.ItemIndex).Delete()  
    Session("Cart") = objDT  
 
    dg.DataSource = objDT  
    dg.DataBind()
 
End Sub
So this used to remove a row from my data grid, but not anymore.

It would be super if you knew what the problem was.





Similar Threads
Thread Thread Starter Forum Replies Last Post
session in shopping cart page Jane Siu Beginning PHP 1 November 22nd, 2006 01:49 AM
Problem with session on Shopping Cart comicghozt .NET Framework 1.x 6 September 21st, 2006 09:22 AM
Session & secuirty issue of shopping cart apple007 ASP.NET 1.0 and 1.1 Basics 0 July 25th, 2006 01:44 AM
can we manage shopping cart without using session pradeep1976 BOOK: Beginning ASP 3.0 0 April 7th, 2005 05:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.