Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Values to the same session, from different pages 1

Status
Not open for further replies.

richfield

IS-IT--Management
Jan 6, 2005
23
GB
Hi,

I'm in the process of desingning a shopping cart and am having difficulty with the following, basically there is various pages with datagrids of items you can buy, on each page there is a hyperlink to another shop, the problem is when you click on the hyperlink it creates a new session as opposed to adding to it? so the items I bought from store1 will be deleted when I click the hyperlink to store2, I think it has something to do with my page load event? Any ideas?

Code:

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

Dim objleathers As New leathers.OraclePhatService 'Declare variable for webservice proxy class
Dim ds As DataSet 'declare variable for dataset

ds = objleathers.GetPhatAll() 'fill dataset with Data from GetPhatAll function
dgList.DataSource = ds 'Set datasource of datagrid with data set data
dgList.DataBind() 'bind data
ddlProducts.DataSource = ds
ddlProducts.DataBind() 'bind data
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


Thanks in Advance

Rich
 
the problem is when you click on the hyperlink it creates a new session as opposed to adding to it
I think that this probably isn't the case - it is most likely that you are overwriting the exisiting shopping cart when you do:

Code:
Session("Cart") = objDT

As you have only posted code from one page this is a bit of a guess but it is my first impression of what is going wrong. If this is the case then you would need to update the shopping cart rather than set it. This could be done quite easily by using a Collection rather than a DataTable.



----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
The code for the second page is exactly the same apart from there is no makeCart() function, the shopping cart gets cleared when you go back to shop1 using the hyperlink, is there anycode example you can show me how to update the shopping cart like you said in your post?

Thanks
Rich
 
Certainly...

It's a very simple example of how you could use a session variable to hold a collection. You will need to create a web page with two buttons.

Code:
Private Sub WriteCartContents()
        Dim col As Collection
        Dim CurrentItem As String

        ' Set our collection to the session cart
        col = Session("MyCart")

        ' Write out each item
        For Each CurrentItem In col
            Response.Write(CurrentItem & "<br>")
        Next
    End Sub

    Private Sub AddToCart(ByVal ItemName As String, ByVal ItemPrice As Double)
        Dim MyCollection As New Collection

        ' Check if the session variable exists
        ' If it does set the collection to it
        If Not Session("MyCart") Is Nothing Then
            MyCollection = Session("MyCart")
        End If

        ' Add the item
        MyCollection.Add(ItemName & "," & ItemPrice)

        ' Update the session variable
        Session("MyCart") = MyCollection
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Add another product
        AddToCart("Product1", 10)

        ' List the contents of the cart
        WriteCartContents()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ' Add another product
        AddToCart("Product2", 10)

        ' List the contents of the cart
        WriteCartContents()
    End Sub

The code should be self-explanatory but please post if you don't understand any of it.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top