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; } }
|