locked
Clone of Session Object RRS feed

  • Question

  • Hi,

    i have a class. I am using the list<myclass> and saving the list to the session.

    When i am retrieving the data from the session and modifies it , it modifies the session object value also. I need a fresh list<myclass> each time i retrieve data from session. Here is my code

                

     List<myclass> lstmyclass = new List<myclass>();
                lstmyclass = (List<myclass>)Session["Listlstmyclass "];

    Thanks in Advance.


    Rohan Tomar

    Tuesday, September 18, 2012 9:36 PM

Answers

  • This list that you storing in Session, you need store then between the pages? If not, you can use ViewState instead Sessions.

    Web Developer

    • Marked as answer by Rohan Tomar Thursday, September 20, 2012 3:12 PM
    Thursday, September 20, 2012 2:11 PM
  • Are you modifying the list, or an item in the list?  If the latter, and you do not want the session to reflect that, then in the code I gave, you need to make a copy of the "myclass" object.  If you have a copy constructor, you would use:
     
    List<myclass> lstmyclass = new List<myclass>();
    foreach (myclass mc in (List<myclass>)Session["Listlstmyclass "])
        lstmyclass.Add(new myclass(mc));
     

    --
    Mike
    • Marked as answer by Rohan Tomar Friday, September 21, 2012 4:46 PM
    Thursday, September 20, 2012 9:55 PM
  • If you modify something stored in session state, then the session changes.  If you want to modify the list after getting it from session, then you should duplicate the list from session to a new list, cloning individual objects (myclass) within the list.
     

    --
    Mike
    • Proposed as answer by Alan_chen Wednesday, September 19, 2012 2:44 AM
    • Marked as answer by Jason Dot Wang Wednesday, September 26, 2012 8:25 AM
    Tuesday, September 18, 2012 9:56 PM
  • Like this:
     
    List<myclass> lstmyclass = new List<myclass>();
    foreach (myclass mc in (List<myclass>)Session["Listlstmyclass "])
     lstmyclass.Add(mc);

    --
    Mike
    • Proposed as answer by Jason Dot Wang Thursday, September 20, 2012 6:07 AM
    • Marked as answer by Jason Dot Wang Wednesday, September 26, 2012 8:25 AM
    Wednesday, September 19, 2012 9:16 PM

All replies

  • If you modify something stored in session state, then the session changes.  If you want to modify the list after getting it from session, then you should duplicate the list from session to a new list, cloning individual objects (myclass) within the list.
     

    --
    Mike
    • Proposed as answer by Alan_chen Wednesday, September 19, 2012 2:44 AM
    • Marked as answer by Jason Dot Wang Wednesday, September 26, 2012 8:25 AM
    Tuesday, September 18, 2012 9:56 PM
  • Yes i want to modify the list after getting it from session. What do you mean by "duplicate the list from session to a new list". Can you provide me some code?

    Rohan Tomar

    Wednesday, September 19, 2012 12:55 PM
  • Like this:
     
    List<myclass> lstmyclass = new List<myclass>();
    foreach (myclass mc in (List<myclass>)Session["Listlstmyclass "])
     lstmyclass.Add(mc);

    --
    Mike
    • Proposed as answer by Jason Dot Wang Thursday, September 20, 2012 6:07 AM
    • Marked as answer by Jason Dot Wang Wednesday, September 26, 2012 8:25 AM
    Wednesday, September 19, 2012 9:16 PM
  • Thanks Family Tree Mike.

    it didn't work for me. If i am modifying lstmyclass, next time  i get the modified data from session.


    Rohan Tomar

    Thursday, September 20, 2012 2:09 PM
  • This list that you storing in Session, you need store then between the pages? If not, you can use ViewState instead Sessions.

    Web Developer

    • Marked as answer by Rohan Tomar Thursday, September 20, 2012 3:12 PM
    Thursday, September 20, 2012 2:11 PM
  • No i don't need to store that in between the pages. i am using that in a class.

    Rohan Tomar



    • Edited by Rohan Tomar Thursday, September 20, 2012 2:26 PM
    Thursday, September 20, 2012 2:26 PM
  • If you do not want to modify session, you can retrive your value form session as a list, and copy it into a new list.

            Session["list"] = new List<string>() { "a", "b" };
            List<string> list = (List<string>)Session["list"];
            List<string> newList = new List<string>(list);
            newList[0] = "test";
    Note if you see value from Session it will keep a and b as values.


    Web Developer

    Thursday, September 20, 2012 2:42 PM
  • Thanks for the reply Norkk. I just tested your code and it Does not work. Session value is modifying when i am modifying newlist.

    Rohan Tomar

    Thursday, September 20, 2012 2:53 PM
  • Are you modifying the list, or an item in the list?  If the latter, and you do not want the session to reflect that, then in the code I gave, you need to make a copy of the "myclass" object.  If you have a copy constructor, you would use:
     
    List<myclass> lstmyclass = new List<myclass>();
    foreach (myclass mc in (List<myclass>)Session["Listlstmyclass "])
        lstmyclass.Add(new myclass(mc));
     

    --
    Mike
    • Marked as answer by Rohan Tomar Friday, September 21, 2012 4:46 PM
    Thursday, September 20, 2012 9:55 PM
  • Awesome !!!!... Thanks Family Free Mike....

    Rohan Tomar

    Friday, September 21, 2012 4:46 PM