Subject: Problems with Sessions
Posted By: HelpWanted Post Date: 12/6/2006 7:19:06 AM
Hi

I am having problems with Sessions, for which is now starting to drive me slightly barmy, this is to keep the session when moving from page to page so that when returning to a cart the items are still in place that have been added.

Please see the code below in which I am trying to resolve: -

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

Sub Page_Load(s As Object, e As EventArgs)
    objDT = New System.Data.DataTable("Cart")
    objDT = Session("Cart")
    Session("Cart") = objDT
     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 = ddlProducts.SelectedItem.Text
    Dim blnMatch As Boolean = False

    For Each objDR In objDT.Rows
        If objDR("Product") = Product Then
            objDR("Quantity") += txtQuantity.Text
            blnMatch = True
            Exit For
        End If
    Next

    If Not blnMatch Then
    objDR = objDT.NewRow
    objDR("Quantity") = txtQuantity.Text
    objDR("Product") = ddlProducts.SelectedItem.Text
    objDR("Cost") = Decimal.Parse(ddlProducts.SelectedItem.Value)
    objDT.Rows.Add(objDR)
    End If
    Session("Cart") = objDT

    dg.DataSource = objDT
    dg.DataBind()
    
    lblTotal.Text = "£" & GetItemTotal()
End Sub

Function GetItemTotal() As Decimal
    Dim intCounter As Integer
    Dim decRunningTotal As Decimal
    
    For intCounter = 0 To objDT.Rows.Count - 1
        objDR = objDT.Rows(intCounter)
        decRunningTotal += (objDR("Cost") * objDR("Quantity"))
    Next
    
    Return decRunningTotal
End Function

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()

     lblTotal.Text = "£" & GetItemTotal()
End Sub

Any help with this would be greatly appreciated.

Thanks
Sharon
Reply By: HelpWanted Reply Date: 12/7/2006 3:48:29 PM
I seem to have resolved part of the problem with this now, which seems to be from the Page_Load section of the code: -

Private Sub Page_Load(ByVal Sender As System.Object, ByVal E As System.EventArgs)
    If Not IsPostBack Then
        objDT = New DataTable()
         makeCart()
        Session("Cart") = objDT
        
        dg.DataSource = (CType(Session("Cart"), DataTable))
        dg.DataBind()
    End If
End Sub

It seems to have resolved the issue of part of the table disappearing but the data is still not staying in the session so still plodding on with this, again any suggestions would be grateful.

Thanks

Sharon
Reply By: dparsons Reply Date: 12/7/2006 3:56:43 PM
IMHO, this is a poor way to manage a shopping cart simply because you pass the entire cart around in session.  This can become problematic (outside of the problme you are having) and cause your server preformance to grind because you are storing all of that information in session.  (Consider 10000 users at your site all at once with shopping carts that each contain 30 unique items consider still, if the user doesnt log out and just leaves the page, the application will hold onto that session in memory until the session timesout.)

You are already storing the information in a database, I would suggest that you keep the cart there and just pass a cartID around in session that relates back to a row in your database, this is much more effecicent.

In so far as why your session is being lost, I can't see anything that stands out, I might suggest stepping through your code to see what the compiler is doing.  

hth.

-------------------------
I will only tell you how to do it, not do it for you.  
Unless, of course, you want to hire me to do work for you.

^^Thats my signature

Go to topic 52919

Return to index page 99
Return to index page 98
Return to index page 97
Return to index page 96
Return to index page 95
Return to index page 94
Return to index page 93
Return to index page 92
Return to index page 91
Return to index page 90