Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 6th, 2006, 08:19 AM
Authorized User
 
Join Date: Sep 2006
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problems with Sessions

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
  #2 (permalink)  
Old December 7th, 2006, 04:48 PM
Authorized User
 
Join Date: Sep 2006
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
  #3 (permalink)  
Old December 7th, 2006, 04:56 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
redirect with SSL/cookieless sessions problems jtyson ASP.NET 1.0 and 1.1 Basics 2 May 29th, 2013 07:32 PM
chapter 16 - Problems with sessions. kb90 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 3 April 11th, 2008 09:07 AM
Problems with sessions in php 4.3.11 ocabrera70 Beginning PHP 1 January 31st, 2008 04:28 AM
sessions problems: change on refresh ! papango Beginning PHP 4 July 28th, 2004 11:08 AM
Sessions - one user has problems kc5f Beginning PHP 7 June 8th, 2004 07:43 PM



All times are GMT -4. The time now is 03:13 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.