Showing posts with label directory. Show all posts
Showing posts with label directory. Show all posts

Thursday, March 29, 2012

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"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" <anonymous@.discussions.microsoft.com> wrote in message
news:CE2AB820-0B7C-43E0-BCA6-E3A654E6E18A@.microsoft.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
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:%23tyjDyLTEHA.2336@.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" <anonymous@.discussions.microsoft.com> wrote in message
> news:CE2AB820-0B7C-43E0-BCA6-E3A654E6E18A@.microsoft.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

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 name
space.
My hypothesis is that it is because this directory is compiled before the di
rectory that actually has the Session variable's declaration. Any ideas on
how to fix this?
thanks.
George"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" <anonymous@.discussions.microsoft.com> wrote in message
news:CE2AB820-0B7C-43E0-BCA6-E3A654E6E18A@.microsoft.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
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:%23tyjDyLTEHA.2336@.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" <anonymous@.discussions.microsoft.com> wrote in message
> news:CE2AB820-0B7C-43E0-BCA6-E3A654E6E18A@.microsoft.com...
> login page. In another .cs file (in another directory), I'm trying to use
> namespace.
the
> directory that actually has the Session variable's declaration. Any ideas
> on how to fix this?
>

Monday, March 26, 2012

Session variable is not transferred to an asp page?

I have a asp.net project with all major pages written in aspx, but I want to keep an existing asp page in the same directory as the aspx files. But when the asp page gets loaded, the session variable is not recognized. Is this the case and why?To be specific, when I load the asp.net app using the address of http://localhost/myproj/ it works fine, and the asp page could read the session variable assigned by an aspx page. But if I use my computer name, say http://keystrokes_win2k/myproj/ then it doesn't work.

Any ideas?
No, the ASP and ASP.NET sessions are two separate objects that do not share their data. There are some third party session objects that you can use to communicate between the two environments, though.
What is the third party session object, please provide some lead ?

session variable lost across different folders

Hi

A session variable that I have created can only be shared by the pages within the same directory where the page containing the session variable is sitting. Once I redirect the page to another page outside the folder (a different folder), the session variable is lost (empty). I understood that a session variable is meant to be used across all files in an application. Am I missing something here? Or I didn't setup properly?

Please help.

ThanksIs is useable throughout and application. IE if you have an application directory called /myapp then all folders under that directory will accept the session variable. (I think). There are a number of ways to get around this. If you tell me were the 2 differnt folders are located,(relative to eachother). and what kind of value you are trying to pass I'll be able to help you.

One last thing: what kinda of script are you trying to write? login page, shopping cart, etc...

Ryder
Thanks Ryder!

I have an application folder called 'QSAConnect' and under this folder there are several sub-folders. The page with the session variable called Session("StudentID") is saved in a sub-folder called 'AbsRecordEntry', and I want to redirect this page to another page, called absreport.aspx, which requires this Session("StudentID") as a passing parameter for all the functions. The target page(absreport.aspx) is located in a sub-directory called 'AbsReport'. Here is the code:


Sub btnViewReport_Click(sender As Object, e As EventArgs)
Dim strStudentID as string
strStudentID=lbxStudentLastName.Items(lbxStudentLastName.SelectedIndex).Value
Session("StudentID") = strStudentID
response.redirect(".../AbsReport/absreport.aspx")

End Sub


This produced an empty session variable when the absreport.aspx loaded. But if I place the file absreport.aspx under AbsRecordEntry folder and change code to:

response.redirect("absreport.aspx")

there is no problem.

I hope you can understand my explanation.

Thanks again

Haiyi
hmmm, I created a similar situation on my testing server a few minutes ago and it worked fine. But what I did notice is that i wrote my response.redirect line a bit differntly and I think that may be it.

see, here is how you wrote yours: response.redirect(".../AbsReport/absreport.aspx")
and this is how I wrote mine: response.redirect("../AbsReport/absreport.aspx")

notice I only have 2 periods not 3. So my next step was to try my code with 3 periods. It generated an error, so i beleive that may be it. The only onther thing I could think of off the top of my head was that maybe the folder AbsRecordEntry or AbsReport is also an application directory. because this would cause a problem perhaps.

Let me know if this works.

Ryder

Thursday, March 22, 2012

Session Variables

I have a question about session variables. I'm setting a session variable in a file in an applications directory and it stores the variable fine and I can access it. Problem is I have a sub directory called administration that I need to access this session variable when I try to there is no value. Is there a specific syntax for setting session variables that they are accessible in sub-directories or should I be doing something different. Here is my syntax.


Session("UserID") = userVal
Is the sub-directory within the same application? When you are accessing the session variable are you getting an empty string or is the app throwing a null exception?
Yes it is within the same application. It's an application within an application but the subdirectory lies within the other.

Marc