Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am getting List of Object from Session, then Update that list of object in that case session value is automatically updated.

This is the piece of code :-

XML
if(Session["StudentData"]!=null)
        {
            List<Student> testStudent = Session["StudentData"] as List<Student>;
            Response.Write("<br>Value in Session <br/>");
            DisplayData(testStudent);

            int i = 0;
            foreach (Student obj in testStudent)
            {
                if (i == 2)
                {
                    obj.Name = "Adi";
                    obj.Age = 23;
                    break;
                }
                i = i + 1;
            }

            Response.Write("<br>Modify Value in Session <br/>");
            DisplayData(testStudent);

            Response.Write("<br>Value in Session <br/>");
            DisplayData(Session["StudentData"] as List<Student>);




        }


How to avoid this ?
Why this happens ? Please explain in detail.
Waiting for your Reply.

Thanks.
Posted
Updated 25-Mar-16 8:58am

For future reference:

In instances like this, a copy constructor is typically preferred when working with objects stored in session without updating the object in session.


@MSDN How to: Write a Copy Constructor


Cloning isn't preferred in the majority of situations due to lack of specified cloning depth (deep vs shallow).

In your case, a collection class with a copy constructor or
C#
.ToList<t>();</t>
could be used to obtain a copy.


C#
var studentsInSession = Session["Student"] as List<Student>
var students = studentsInSession.ToList(); 

students.Add(new Student("John", "Smith"));


This would allow the modification of the list without reflecting the change to the session.
 
Share this answer
 
instead of updating the same list why not take a copy of it yourself.

XML
if(Session["StudentData"]!=null)
        {
            List<Student> testStudent = Session["StudentData"] as List<Student>;
            Response.Write("<br>Value in Session <br/>");
            DisplayData(testStudent);

            int i = 0;
            List<Student> another;
            foreach (Student obj in testStudent)
            {
                another[i] = obj;
                if (i == 2)
                {
                    another[i].Name = "Adi";
                    another[i].Age = 23;
                    break;
                }
                i = i + 1;
            }

            Response.Write("<br>Modify Value in Session <br/>");
            DisplayData(another);

            Response.Write("<br>Value in Session <br/>");
            DisplayData(Session["StudentData"] as List<Student>);




        }
 
Share this answer
 
Comments
[no name] 3-May-12 8:19am    
hi,
Thanks for reply reply.

I tried this but still same issue....session is getting updated.

Thanks.
The session is updated because the variable testStudent is equal to the list in Session["StudentData"]. That is the first statement in you're code : List<student> testStudent = Session["StudentData"] as List<student>;

Remember these are all reference types. So testStudent == Session["StudentData"]. Equally all objects in Session["StudentData"] are in testStudent.

When it is you're goal to change the objects in testStudent list without changing the session, you must clone the objects before add them to a new list, the testStudent list;

To achieve that derive you're Student class from ICloneble and add the following extension class to you're project.

C#
static class Extensions
{
        public static IList<t> Clone<t>(this IList<t> listToClone) where T: ICloneable
        {
                return listToClone.Select(item => (T)item.Clone()).ToList();
        }
}
</t></t></t>


the first statement in you code can the be:

C#
IList<student> testStudent = Session.Clone<student>();
</student></student>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900