C# .NET - app_code class file create session and assign value

Asked By Naveed Mohammed on 05-Mar-13 11:35 PM
hi all,

how to create session in (App_code class .cs file) assign some value.

Thanks. 
Danasegarane Arunachalam replied to Naveed Mohammed on 09-Mar-13 07:22 AM
To use session in app_code folder you need to add reference to the System.Web.SessionState.


In the top of the class file after the final using statement add this using statement

using System.Web.SessionState;


And then you can assign the session value anywhere in the class file as

HttpSession currentsession = HttpContext.Current.Session; // this will hold the reference to session object

Set  the session value as
currentsession["myvalue"] = "Session in app_code file";

And read the session value as

Object mysession=currentsession["myvalue"] ;


Hope this helps