Tweet
SIGN UP
MEMBER LOGIN:
TECHNOLOGIES
.NET 4.5
.NET Remoting in C#
Active Directory C#
ADO.NET in C#
AJAX in C#
Algorithms in C#
Android Programming
Articles C#
ASP, JavaScript, CSS
ASP.NET Controls in C#
ASP.NET MVC with C#
ASP.NET Programming
BizTalk Server
C# Assemblies
C# Language
C# Tutorials
C, C++, MFC
Career Advice
Chapters
Cloud Computing
COBOL.NET
Coding Best Practices
COM Interop
Compact Framework
Cryptography C#
Crystal Reports C#
Current Affairs
Custom Controls C#
Databases & DBA
Deployment
Design & Architecture
DirectX C#
Enterprise Development
Error Zone
Exception Handling C#
Expression Studio
F#
Files, Directories in C#
Financial Applications
Games Programming C#
GDI+ & Graphics
Hardware
How do I
HTML 5
Internet & Web
iPhone/iPad
Java
Java and .NET
JQuery
JSP
Leadership
Learn .NET
LINQ with C#
Metro Style Apps in C#
Mobile & Embedded
MonoDevelop
MSMQ in C#
Multithreading in C#
Networking
Office Development
OOP/OOD
Operating Systems
PHP
Printing in C#
Products
Project Management
Reports using C#
Robotics & Hardware
Security in .NET
SharePoint
Silverlight with C#
Smart Devices
Speech in C#
SQL
SQL Server 2012
String in C#
Team Foundation & VSS
Testing
Visual Basic .NET
Visual C#
Visual Studio .NET
Visual Studio 11
Visual Studio 2010
VS LightSwitch 2011
WCF with C#
Web Forms C#
Web Services in C#
WebForms Controls
Windows 8 in C#
Windows Controls C#
Windows Forms C#
Windows Phone in C#
Windows PowerShell
Windows Services in C#
Workflow Foundation in C#
WPF with C#
XAML with C#
XML in C#
XNA with C#
FORUMS
BLOGS
VIDEOS
INTERVIEWS
CERTIFICATIONS
DOWNLOADS
BOOKS
LINKS
NEWS
Learn .NET in 60 days – Part 1 (13 Labs)
Learn MVC (Model view controller) Step by Step ...
Learn C# Corner - Home
Using Border Radius and Gradients in CSS3: Part I
Learn C# Corner - Footer
Learn .NET and C# in 60 Days Lab13(Day 5): - C ...
iPhone 5 First Look
Samsung Galaxy Note Review
WCF - Authentication and Authorization in Ente ...
How to write a good article on C# Corner
Blog
Update Session Object
Posted by
Dhaval Patel
in
Blogs
|
C# Language
on
Jan 30, 2012
Here you will learn about the update session object.
Tweet
594
0
0
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
share this blog :
Manipulating Huge Integers In C#
Leverage “using” in C#
Related Blogs
Create Custom Message Box and Marshal arcGis Object in C#
Cagdas's Blog
Serializable Dictionary
Features Class Name in ARC Object using C#
IFormattable interface in C#
Equals object method
post comment
Sponsored by
Become a Sponsor
More Blogs from this Blogger
Update Session Object
How to Update Session Object in C#
Create Calander using Sql server Store Procedure
How to Get sum of first record amount and second amount is in Total Amount using Sql server STored Procedure
How to get Week First and Last day and also running week Number using Sql server
How to update grid data without page refresh in c#
Jquery using make change in stylesheet
Jquery using Insert button after page load then make fade in and out
How to Hide url Extension using Global.asax
Search into Textbox and Bind Gridview using Asp.net
View All
Latest Blogs
WebPart Collector or WebPart Finder
Free Ride is Over for Desktop Developers in Visual Studio 11
DotNet developers most used application/tools launching through 'Run'
80-inch Windows 8 Tablet
Data encapsulation
Option to access the Column Names in Data table
Open multiple windows in browser startup. (Multiple homepages option)
I'm Sorry
const and readonly
Address, Binding and Contract in WCF
View All
Sponsored by
Become a Sponsor