Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
Search :       Advanced Search »
Home » Blogs Home » Blog Detail

Update Session Object

 by Dhaval Patel on Jan 30, 2012

Here you will learn about the update session object.
Comments: 0 Views: 533 Printable Version 
Many times we try to make copy of session object, but in case when we are modifying copied object we might noticed that session object gets updated automatically, the reason is both copied object and session object are pointing to same location, even if you tried to use "new" operator while creating object.

Scenario

Let say you have Employee Class as mentioned under

public class Employee
{
     public string Emp_FName { get; set; }
     public string Emp_LName { get; set; }
}


Problem:

Employee objemployee = new Employee();
objemployee.Emp_FName = "Sachin";
objemployee.Emp_LName = "Tendulkar";

Then try to save object in Session

Session["EmployeeObj"] = objEmployee;

This method will work good till we are just accessing object from session, but in case if we try to update object created from session it will update value of session also.

That is,

employee newemployee = new employee(); //Even though object is created using "new" keyword.
newemployee = (Employee) Session["employeeObj"];
newemployee.Emp_FName = "Kapil";
//This will update session employee FirstName also.

Solution:

To make copies of session you need to create a clone method in class.

In above class create a method clone, to support copy of session.

public class Employee
{
     public string Emp_FName { get; set; }
     public string Emp_LName { get; set; }
}
public Member clone()
{       
     Employee cloneEmployee = new Employee();
     cloneEmployee.Emp_FName = this.FirstName;
     cloneEmployee.Emp_LName = this.LastName;
}

Now, while accessing session object, you can call clone method to make copy of session.

Employee newEmployee = new Employee();
newEmployee = ((Employee) Session["EmployeeObj"]).clone();

Now if you try to modify object copied from session, will not update session object.

newEmployee.Emp_FName = "Kapil"; //Will not update session employee FirstName
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
 
What do you say about this post? Post a comment here
*Title:
*Comment:
 
Comments not available.
Nevron Gauge for SharePoint
Become a Sponsor

 Blogger's Profile
Age: Not Available
Location:
Title: Developer
Joined: Mar 26, 2010
Education: Bachelors Degree
 More Blogs from this Blogger
No record available
 Latest Blogs
[Video] OnClose Handler
[Video] Storing and Loading the Window and Toolbar position
The Euclidean Algorithm
Swapping Exe Process
How Exe file is Generated by VS2005 C++ Project?
What is Exe
Header files: Multiple Inclusion problem - Solution B
Header files: Multiple Inclusion problem - Solution A
Header files: Multiple Inclusion problem - Reason
Header files: Multiple Inclusion problem
View all »
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.