Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 5-Jul-10 21:18pm
Edited 14-Jul-10 16:27pm
v2
Comments
Ankur\m/ at 6-Jul-10 2:29am
   
Could you paste the RELEVANT code here?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try using
HttpContext.Current.Session["varName"];
It should work.
  Permalink  
v2
Comments
ramprasad07@hotmail.com at 16-Aug-13 10:35am
   
not works
Ankur\m/ at 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 at 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/ at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

<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();
        }
    }
}
 

  Permalink  
v2
Comments
jaipoint at 17-Dec-13 0:49am
   
hello sir,am getting null value
Ankur\m/ at 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]
makhaai at 25-Jul-10 11:29am
   
Sorry, I am new in CP. will add edit note when I edit any others answers. Thank you
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Chris Maunder at 14-Jul-10 22:17pm
   
Note quite: it's also available, as a previous answerer noted, in HttpContext.Current
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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 )
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 562
1 Sergey Alexandrovich Kryukov 484
2 Maciej Los 325
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100