4

Imagine that I have an instance (oEmp) of "Employee" class and I would like to store it session.

Session["CurrentEmp"] = oEmp;

If I modify a property in oEmp as follows:

oEmp.Ename = "Scott";

Am I referring to session item through above statement or just only "oEmp"?

Session["CurrentEmp"] = oEmp; //Do we still need this after any property is modified

Is that the same case, if I opted for SQL Server session state (instead of InProc).

thanks

1

3 Answers 3

7

Asp.net Session will hold the reference, so you shouldn't need to do the following:

Session["CurrentEmp"] = oEmp;

after modifying oEmp;

Sign up to request clarification or add additional context in comments.

1 Comment

Joe, your answer contradicts with ARS. I am confused now :(
2

Session Variables are held as reference types so there is no need to update its value every time.
You object instance that you store, only the reference to that object is stored in the session variable.

Here are some link to help you find more details

http://bytes.com/topic/asp-net/answers/447055-reference-types-session

http://forums.asp.net/t/350036.aspx/1

Do asp.net application variables pass by reference or value?

Comments

2

I am updating my response as my understanding of session data serialisation was not correct. I am not going to delete this answer as it might help other understand how session works. I would thank @Guru for point this out.

Irrespective of session mode, session data is updated back to session object only when the request is successful. So if you have assigned a reference object to session and then update the object in the same request, the session will hold the updated information.

Refer: Underpinnings of the Session State Implementation in ASP.NET for more information

6 Comments

ARS, your answer contradicts with Joe. I am confused now :(
@ARS Can you please provide some link or resource explaining this ? I am quiet surprised by this actually, the way Sessions behaves should not be affected by underlying place where it is stored
@Guru: I have respond to the query as per my understanding. However you can Refer to msdn.microsoft.com/en-us/library/aa479041.aspx and check Table 1. State client providers. Where it mention about the serialization of data in case of Out-Of-Proc. Also I believe once you have serialised an object you wont be able to change the state of it unless you are update the session data.
MSDN: "The binding between the session state values and the session object visible to developers lasts until the end of the request. If the request completes successfully, all state values are serialized back into the state provider and made available to other requests." So the data is serialized back after the request is complete. So no need to update your session variables.
@ARS - I agree with this - "once you have serialised an object you wont be able to change the state of it..." - but I don't think it is actually serialized until it is saved.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.