Click here to Skip to main content
15,915,319 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi,

In my project, I have a Session variable defined in a Log-in page. If I try to use it in another .cs file (which doesn't have a corresponding .aspx file), it says: "The name 'Session' does not exist in the current context".
Can anyone please tell me how can I access the Session variable.

Thanks in advance.
Posted
Updated 14-Jul-10 15:27pm
v2
Comments
Ankur\m/ 6-Jul-10 2:29am    
Could you paste the RELEVANT code here?

Try using
C#
HttpContext.Current.Session["varName"];

It should work.
 
Share this answer
 
v2
Comments
ramprasad07@hotmail.com 16-Aug-13 10:35am    
not works
Ankur\m/ 17-Aug-13 11:51am    
What doesn't work?
It certainly works as I have used it in a scenario described above. Moreover it worked for OP as the answer was accepted.
ramprasad07@hotmail.com 19-Aug-13 10:44am    
my problem is that HttpContext.Current itself is null even i pass value through session in aspx page and when comes here it always tells the object reference because httpcontext.current is null
Ankur\m/ 20-Aug-13 2:53am    
Depends where you are using it. Read about it in MSDN. Also search Google/Bing for 'httpcontext.current always null' and check out the discussions.
The property you ar trying to acces is the Session[^] property. This property is only defined in a Page[^] class. So in order to access your session objects you must use a coresponding page class.
 
Share this answer
 
Comments
Chris Maunder 14-Jul-10 22:17pm    
Note quite: it's also available, as a previous answerer noted, in HttpContext.Current
<pre lang="cs">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.SessionState;
/// <summary>
/// Summary description for Class1
/// </summary>
///IRequiresSessionState enable session in Class file
public class Class1:IRequiresSessionState
{
    public Class1()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public void getSession(string key)
    {
        if (HttpContext.Current.Session[key] != null)
        {
            string sessionID= HttpContext.Current.Session[key].ToString();
        }
    }
}



 
Share this answer
 
v2
Comments
jaipoint 17-Dec-13 0:49am    
hello sir,am getting null value
Ankur\m/ 7-Jul-10 3:10am    
Reason for my vote of 1
You method will always check for Session["Key"]. It is not a generic method and how does it solve OP's query?
[Edit]
Seems you have modified it now. You should add an edit note in such cases.
[/Edit]
raju melveetilpurayil 25-Jul-10 11:29am    
Sorry, I am new in CP. will add edit note when I edit any others answers. Thank you
I'm not sure you've been given a real answer. To access the session in your external class, you need to pass the Session object through to it. When you type Session, it's not a global object, it's accessing this.Session. That's what you need to pass through if you can't just pass through the value ( which would be better )
 
Share this answer
 

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