Cannot Read Changed Session Value?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sudzemeckis
    New Member
    • Jul 2007
    • 13

    #1

    Cannot Read Changed Session Value?

    Hi guys, i am a freshman here.. I would like to ask a things about Session.. Does session only work to be read for once? and if you change it into new value, it will not be able to be read in other page?

    Scenario..
    I have page A and Page B. I assign a new Session("cart") = 1. Then I can read in Page B that Session("cart") = 1.

    Due to some reason, I have changed the Session("cart") = 2 in page A. Then i go back to Page B again. When I read the Session("cart") in Page B, it still contain the old value which is 1.

    Is that the Session actually work? Is there anyway to solve the problem?

    Thanks for any help..
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Are you sure you are in the same session? And don't accidently have two?

    What does your code look like? If it is the same session you should be able to make changes and view as many times as you want.

    Comment

    • sudzemeckis
      New Member
      • Jul 2007
      • 13

      #3
      Thank for your reply...

      Yeah i am sure.. here is piece of my code...

      =============== ============Pag e A============== ==========
      If IsNothing(Sessi on("cart")) Then
      Dim sql As String
      sql = #SQL CODE HERE#
      cart = GetListRecord(s ql) <---- A method that return ArrayList
      Session("cart") = cart <---- Session Assignment
      Else <----------- this part is to add more data to Arraylist
      Dim tempCart As New ArrayList
      tempCart = Session("cart")
      Dim sql As String
      sql = #SQL CODE#
      tempcart = AddToCart(SQL,t empCart) <--- return added cart
      Session("cart") = tempCart
      End If



      =============== ==========PAGE B============== ==========

      Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
      Alert(Session.I tem("Cart").cou nt()) <--- prompt number of item in Arraylist
      End Sub


      *NB alert(*String*) <---- is a method i used to prompt on ASP...
      =============== =============== =============== ===========

      for the first assignment, i can read in PAge B... but when i go back to page A, then changed the session value, then go back to page B, it still prompt old value...

      Thank for your reply...

      Comment

      • TRScheel
        Recognized Expert Contributor
        • Apr 2007
        • 638

        #4
        Aren't session items returned as objects? When you get the count, it is counting how many objects there are in that session item (which, to be honest, doesn't seem like correct code to begin with) and that should be one, an array. If you cast it to an array, then retrieved the count, it should give you the correct value.

        Although, then again, I could be way off.

        Comment

        • sudzemeckis
          New Member
          • Jul 2007
          • 13

          #5
          Thanks...

          Yeah.. it return number of item that i store in arraylist... It is same.. whenever i add new item to the arrayList in page A... and assign it to the Session... At the page b, it only return the number of item in the arraylist that i stored for the FIRST time...

          I dont know why.. but it is appear... T.T

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            I believe you need to cast the object contained in Session as their correct types before you can use them correctly.

            Also, are you sure your SQL is correct? The code you posted does not show the value being increased.

            Perhaps set break points there and make sure it's returning a different value?
            Then you can add things to your WATCH list in VS and see what they really are

            Comment

            • sudzemeckis
              New Member
              • Jul 2007
              • 13

              #7
              Yeah.. i have tried to insert as the break point...

              Example:

              First time i only insert 2 item into the arraylist.. then assign it into the Session... After the assignment, i prompt the session.count() and it appear 2.. ok it is correct anyway..

              then I go to page B, and it prompt 2 as well... yeah!! correct....

              I then try to go back to page A to add more things to my cart... then it prompt 4 due to i add 2 more things....

              then i go back again to see my "pushcart". .. OH MY GOD!!!! it only prompt 2...
              Thats it...

              I am sure that my SQL is working perfectly, because i can retrieve the data that i want..

              Comment

              • TRScheel
                Recognized Expert Contributor
                • Apr 2007
                • 638

                #8
                Originally posted by sudzemeckis
                Thanks...

                Yeah.. it return number of item that i store in arraylist... It is same.. whenever i add new item to the arrayList in page A... and assign it to the Session... At the page b, it only return the number of item in the arraylist that i stored for the FIRST time...

                I dont know why.. but it is appear... T.T
                Try this amigo

                [code=cpp]
                public static class TestClass
                {
                public static object TestVariable;
                }
                // ... elsewhere in your code

                TestClass.TestV ariable = GetListRecord(s ql);
                cart = GetListRecord(s ql);
                [/code]

                That TestVariable should act EXACTLY like your session variable. If you note though, it is an object, and objects do not have a count property. If you tried to grab its count, it should throw an error.

                What you need to do is this:

                [code=cpp]
                ((array)TestCla ss.TestVariable ).Length
                [/code]


                in Vb .Net it would probably look something like:

                [code=vbnet]
                Sub Main()
                Dim Sql As Integer = 3
                Dim cart As ArrayList
                Static TestVariable As Object
                TestVariable = GetListRecord(S ql)
                cart = GetListRecord(S ql)

                'DirectCast(Tes tVariable, ArrayList).Coun t is the correct way to grab the count
                'In this case, TestVariable is taking the place of your session variable because I did this as a desktop app
                End Sub

                Function GetListRecord(B yVal Sql As Integer) As ArrayList
                Dim fakeArray As ArrayList = New ArrayList()
                fakeArray.Add(1 )
                fakeArray.Add(2 )

                Return fakeArray
                End Function
                [/code]

                Comment

                • sudzemeckis
                  New Member
                  • Jul 2007
                  • 13

                  #9
                  Thank you... i learn a new thing which is directcast.. but i was able to show the quantity of Arraylist. It just that the quantity is not relevant between PAGE A and PAge B... =(

                  Now the point is that i am doing my PUSHCART feature... I used session to stored the cart(Arraylist) over page... I need to add the item to the cart in Product Page... and display it in PushCart Page... when i add EXTRA item to the cart in Product PAge, it is not displayed in the PushCartPage and i had made sure that On Product Page it is added. Because the Quantity is relevant. but on PushCart page, it is not showing so..


                  Thank you for your help... Sorry for troubling so much.. =)

                  Comment

                  • TRScheel
                    Recognized Expert Contributor
                    • Apr 2007
                    • 638

                    #10
                    Without seeing more code, this really sounds like a ByVal / ByRef problem, if it is in fact being assigned at all the right times.

                    If you grab an item ByVal and change it, it wont reflect on the parent function. So I could go:

                    [code=vbnet]
                    ChangeCart(cart )

                    Sub ChangeCart(ByVa l cart as Integer)
                    cart += 1000
                    End Sub
                    [/code]

                    It LOOKS like cart is changed, but it isnt. Check around for that in your code. If you still cant figure it out, can you please post more code? And if so, use code brackets which are done like:

                    {code=vbnet}
                    YOUR CODE IN HERE
                    {/code}

                    where { and } are replaced with [ and ] respectively

                    Comment

                    • sudzemeckis
                      New Member
                      • Jul 2007
                      • 13

                      #11
                      Page of Product

                      [code=vbnet]
                      Private Sub btnCustUpdate_C lick(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnCustUpdate.C lick
                      Dim cart As New ArrayList
                      If IsNothing(Sessi on("cart")) Then
                      Dim tempprice As String
                      Dim sql As String
                      sql = "Select Product_ID, Title, " & Request.Form("t xtCustQuant") & " as [CartQuant], Price, price * " + Request.Form("t xtCustQuant") + " as [TotalPrice] From Product, ProductDesc where Product.Categor y = ProductDesc.Cat egory and Product_ID = '" + txtId.Text + "'"

                      cart = GetListRecord(s ql)
                      Session("cart") = cart
                      Else
                      Dim tempCart As New ArrayList
                      tempCart = Session("cart")
                      Dim sql As String
                      sql = "Select Product_ID, Title, " & Request.Form("t xtCustQuant") & " as [CartQuant], Price, price * " + Request.Form("t xtCustQuant") + " as [TotalPrice] From Product, ProductDesc where Product.Categor y = ProductDesc.Cat egory and Product_ID = '" + txtId.Text + "'"
                      tempCart = AddCartData(sql , tempCart)
                      Session("cart") = tempCart
                      End If
                      Alert(Session(" cart").count())

                      End Sub
                      [/code]


                      Page of PushCart

                      [code=vbnet]
                      Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load

                      If Not Session.Item("A ccess") = "Customer" Then
                      Response.Redire ct("home.html" )
                      ChangeLogPage(" login.aspx")
                      Else
                      dgCart.DataSour ce = Session("cart")
                      dgCart.CurrentP ageIndex = 0
                      dgCart.DataBind ()
                      End If
                      End Sub

                      [/code]

                      The external Function

                      [code=vbnet]

                      Public Function GetListRecord(B yVal sql As String) As ArrayList
                      Dim ListData As New ArrayList
                      Dim DB As New DBcon
                      DB.InitializeCo mmand(sql)
                      DB.read()
                      For Each row As Object In DB.objDR
                      ListData.Add(ro w)
                      Next
                      DB.Dispose()
                      Return ListData
                      End Function

                      Public Function AddCartData(ByV al sql As String, ByVal cart As ArrayList) As ArrayList
                      Dim DB As New DBcon
                      DB.InitializeCo mmand(sql)
                      DB.read()
                      For Each row As Object In DB.objDR
                      cart.Add(row)
                      Next
                      DB.Dispose()
                      Return cart
                      End Function

                      [/code]


                      I hope the code will help... thank you.. thanks for help me figuring this out...

                      Comment

                      • TRScheel
                        Recognized Expert Contributor
                        • Apr 2007
                        • 638

                        #12
                        This is more for me than for you, although you should pay attention. I cleaned up redundant code so its easier to narrow down the problem. I suggest reading through and trying to understand why and how I could do what I did

                        [code=vbnet]
                        Private Sub btnCustUpdate_C lick(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnCustUpdate.C lick
                        Dim sql As String = "Select Product_ID, Title, " & Request.Form("t xtCustQuant") & " as [CartQuant], Price, price * " + Request.Form("t xtCustQuant") + " as [TotalPrice] From Product, ProductDesc where Product.Categor y = ProductDesc.Cat egory and Product_ID = '" + txtId.Text + "'"

                        Session("cart") = AddCartData(sql , IIf((Session("c art") Is Nothing), New ArrayList(), DirectCast(Sess ion("cart"), ArrayList)))

                        Alert(DirectCas t(Session("cart "), ArrayList).Coun t())

                        End Sub

                        'Page of PushCart
                        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
                        If Not Session.Item("A ccess") = "Customer" Then
                        Response.Redire ct("home.html" )
                        ChangeLogPage(" login.aspx")
                        Else
                        dgCart.DataSour ce = DirectCast(Sess ion("cart"), ArrayList)
                        dgCart.CurrentP ageIndex = 0
                        dgCart.DataBind ()
                        End If
                        End Sub

                        'The external Functions
                        Public Function GetListRecord(B yVal sql As String) As ArrayList
                        Return AddCartData(sql , New ArrayList())
                        End Function

                        Public Function AddCartData(ByV al sql As String, ByVal cart As ArrayList) As ArrayList
                        Dim DB As New DBcon
                        DB.InitializeCo mmand(sql)
                        DB.read()
                        For Each row As Object In DB.objDR
                        cart.Add(row)
                        Next
                        DB.Dispose()
                        Return cart
                        End Function
                        [/code]


                        Now its clear to see when and how Session("cart") is accessed and set.

                        Try this code, and if it still doesnt work, Alert before and after every access of Session("cart")
                        EDIT: And post the results of those alerts

                        Comment

                        • sudzemeckis
                          New Member
                          • Jul 2007
                          • 13

                          #13
                          Alright... it seems that the problems hadn't been solved...

                          In the Product page, i can prompt the number of items in the arrayList correctly.. I add 2 item, then it prompt 2...

                          At the PushCart page, i can display the correct amount of items inside the arraylist correctly, which is 2...

                          Then i tried to add more 2 items in the product page and it prompt 4 on PRODUCT Page, and go back to the PUSH CART page again, but it just display the OLD 2 items that i had added.

                          hmm.. I had consult with my lecture, and he said that the session will not work as Variable... He said that For the first time of PushCart page has been load, it will work perfectly, but not for the second time. Page PushCart can only read the value of the session in the Page Product when Page PushCart is load for the FIRST time.. else than that, it will not read the new value anymore...

                          He suggest to use cookies than Session, but cookies can't store arraylist.. ARggh.. Confusing.....

                          Comment

                          • TRScheel
                            Recognized Expert Contributor
                            • Apr 2007
                            • 638

                            #14
                            Bizarre as this may be, why dont you try:

                            [code=vbnet]
                            Dim tempCart as ArrayList = DirectCast(Sess ion.Item("cart" ), ArrayList)
                            Session.Remove( "cart")
                            // Change tempCart here
                            Session.Add("ca rt", tempCart)
                            [/code]

                            Use that bit to change the cart, heck you could even throw it into a function so its a one line bit:

                            [code=vbnet]
                            sub ChangeCart(Arra yList NewValues, Session CurrentSession)
                            dim TempCart as ArrayList = DirectCast(Curr entSession.Item ("cart"), ArrayList)
                            CurrentSession. Remove("cart")
                            TempCart.AddRan ge(NewValues.To Array())
                            CurrentSession. Add("cart", TempCart)
                            end sub

                            //... some where else in code
                            ChangeCart(MyNe wArrayList_Full OfNewItems, Session)
                            [/code]

                            Comment

                            • sudzemeckis
                              New Member
                              • Jul 2007
                              • 13

                              #15
                              I think the problem is not on the product page... because whenever i add the NEW value into the session, I prompt the value before changing to page PushCart.. I can read the exactly correct value that i have added... The problem is that on the page PushCart, i cannot read the new value that has been assigned..

                              Comment

                              Working...