473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Session Variable

Hi

In my ASP.NET project, I have a session variable that is assigned on a login page. In another .cs file (in another directory), I'm trying to use
if((string)Session["variable"] == "something"

When I compile, it tells me that Session does not exist in the calss or namespace

My hypothesis is that it is because this directory is compiled before the directory that actually has the Session variable's declaration. Any ideas on how to fix this

thanks

George
Nov 18 '05 #1
2 2613
"Session" is not a global variable, it is a property of the
System.Web.UI.Page object.
When you use Session["variable"] inside an aspx page, it is actually
translating to this.Session["variable"].
When you are in another class which is not an aspx page derived off of the
Page class, it has no access to the Session object.

You either need to pass a reference to the Session object into your other
class (yuck), or pass the value itself as a parameter/property/etc.

--
Michael J. Mooney
MCP+SB, MCAD, MCSD
"George" <an*******@discussions.microsoft.com> wrote in message
news:CE**********************************@microsof t.com...
Hi,

In my ASP.NET project, I have a session variable that is assigned on a login page. In another .cs file (in another directory), I'm trying to use if((string)Session["variable"] == "something")

When I compile, it tells me that Session does not exist in the calss or namespace.
My hypothesis is that it is because this directory is compiled before the directory that actually has the Session variable's declaration. Any ideas
on how to fix this?
thanks.

George

Nov 18 '05 #2
Or you can grab the application object and then access the session:

using System.Web; //Need this at top

HttpApplication ha = HttpContext.Current.ApplicationInstance;

string mySessionValue = ha.Session["MySessionValue"];

Very simple:

VB.NET

Dim ha As HttpApplication = HttpContext.Current.ApplicationInstance
Dim mySessionValue As String = ha.Session("MySessionValue")

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"Michael J. Mooney" <mike_mooney@_yahoo.com.NOSPAM> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
"Session" is not a global variable, it is a property of the
System.Web.UI.Page object.
When you use Session["variable"] inside an aspx page, it is actually
translating to this.Session["variable"].
When you are in another class which is not an aspx page derived off of the
Page class, it has no access to the Session object.

You either need to pass a reference to the Session object into your other
class (yuck), or pass the value itself as a parameter/property/etc.

--
Michael J. Mooney
MCP+SB, MCAD, MCSD
"George" <an*******@discussions.microsoft.com> wrote in message
news:CE**********************************@microsof t.com...
Hi,

In my ASP.NET project, I have a session variable that is assigned on a login page. In another .cs file (in another directory), I'm trying to use
if((string)Session["variable"] == "something")

When I compile, it tells me that Session does not exist in the calss or

namespace.

My hypothesis is that it is because this directory is compiled before

the directory that actually has the Session variable's declaration. Any ideas
on how to fix this?

thanks.

George


Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: doltharz | last post by:
Please Help me i'm doing something i though was to be REALLY EASY but it drives me crazy The complete code is at the end of the email (i mean newsgroup article), i always use Option...
1
by: Ann Leland | last post by:
I have been using session variables to pass a user name from one ASP page to another inside framesets for 9 months and it stopped working this week. I have made no code changes but there was a...
2
by: Eric | last post by:
Hi, I've a problem with trying to retrieve a session variable in an include file. Basically, the main asp creates a session variable: <% Session("var1") = "Hello" %> And then when I click...
4
by: VB Programmer | last post by:
If I have a variable I want to share in my application what is the difference between just declaring a variable (Dim strMyVar as String) and using a session variable (Session("strMyVar"))? When...
9
by: William LaMartin | last post by:
I have a problem, mentioned here before, of Session and Application variables disappearing at one site but not at others or on my development computer. The problem is illustrated by an example...
9
by: Greg Linwood | last post by:
I'm having difficulty understanding Session state in ASP.Net. It's almost embarrassing asking this as I've been using ASP since it was first released & it really shouldn't be this hard to use -...
4
by: T Ralya | last post by:
I am told that ASP.NET controls the session ID and session variables, but that does not fit my symptoms. I am posting here as directed. I'm hoping that someone can at least recommend something to...
3
by: Alan Wang | last post by:
Hi there, Once my application gets complicated and complicated. I found it's really hard to keep track of Session value I am using in my asp.net application. I am just wondering if anyone have...
4
by: Don Miller | last post by:
I am using a Session variable to hold a class object between ASP.NET pages (in VB). In my constructor I check to see if the Session variable exists, and if it doesn't, I create one and populate it...
17
by: Control Freq | last post by:
Hi, Not sure if this is the right NG for this, but, is there a convention for the variable names of a Session variable? I am using .NET 2.0 in C#. I am new to all this .NET stuff, So, any...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.