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.