Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am writing login code in a class file where i need if sqldatreader read the sqlcommand and
if (paswd == dr["password"].ToString()) then i want to store
userId in session like this:
if (paswd == dr["password"].ToString())
{
    Session["userid"]=dr["userid"].ToString();
}

but in class file session is not working.

please any one help me how can i do this.
Thanks in advance
Posted
Updated 31-May-11 1:38am
v2

1 solution

The Session property provides programmatic access to the properties and methods of the HttpSessionState class. Because, ASP.NET pages contain a default reference to the System.Web namespace (which contains the HttpContext class), you can reference the members of HttpContext on an .aspx page without the fully qualified class reference to HttpContext. For example, you can use just Session("SessionVariable1") to get or set the value of the session state variable SessionVariable1. However, class file will not inherit System.web namespace. So we need access like below,
HttpContext.Current.Session["userid"] = dr["userid"].ToString();
 
Share this answer
 
v3
Comments
rahul dev123 31-May-11 7:45am    
i can't declare this in class file. class file not supported session.
rahul dev123 31-May-11 7:47am    
i want to store variable in session at class file
Toniyo Jackson 31-May-11 7:48am    
See the updated answer
rahul dev123 31-May-11 8:13am    
Thanks its working
Toniyo Jackson 31-May-11 8:15am    
You are welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900