views:

287

answers:

2

If I store a String value in my Session variable, do I need to Serialize / Deserialize it?

I read that when you use in your web.config

you need to serialize before you can store the value in session variable

and you would then deserialize when you retrieve the value.

I wonder if for example, you just place the string value to a session like:

Session("MyStringVar") = "MyStringValue"

and when you retrieve it, you could just do:

DIm strVal as String strVal = Ctype(Session("MyStringVar"), String)

And also, is the time out specified for that is 60, is it in minutes or hours?

Thanks

+1  A: 

No. the .net runtime will take care of all of that.

However, you would need to do your own serialization if you were storing an object that wasn't marked as serializable. Also, The timeout value is in minutes.

Chris Lively
So long as an object is Serializable, you don't have to do anything speacial to store it in Session and at a technical level, a string is just another type of object.
Andrew Robinson
@Andrew: You are correct. After rereading my response, I was obviously not using the right words.
Chris Lively
+1  A: 

For the web.config you should be able to do:

String myValue = ConfigurationManager.AppSettings["MyValue"].ToString();

For the Session and a string value you can do, if its not a string, replace string with object type...:

String myValue = (string)Session["MyValue"];
RSolberg