473,239 Members | 1,607 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,239 software developers and data experts.

Global Session Variable

I have a site with an App_Code folder that has Global.asax.cs and a
file named Upload.cs.

I want to pass Upload.cs a Session variable (username) that is set in
default.aspx.

Setting up a session variable in default.aspx is no problem, but how
do I make it available to Upload.cs? I think it's a matter of
writting code into the following two files: Global.asax.cs, and
obviously, Upload.cs, but how exactly is it done?

I know this is a beginner question, and I've TRIED finding the answer,
but somehow it seems like it's harder to find answers to the most
basic questions.

May 16 '07 #1
11 11434
"Dave" <on********@gmail.comwrote in message
news:11*********************@q75g2000hsh.googlegro ups.com...
Setting up a session variable in default.aspx is no problem, but how
do I make it available to Upload.cs?
You don't need to pass Session variables around - they are available to the
entire Session anyway...

string strVariable = HttpContext.Current.Session["MyVariable"].ToString();

--
http://www.markrae.net

May 16 '07 #2
Session variables are available to all pages that are visited by a user in a
single browser session. You get them the same way you set them, or any other
variable.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Dave" <on********@gmail.comwrote in message
news:11*********************@q75g2000hsh.googlegro ups.com...
>I have a site with an App_Code folder that has Global.asax.cs and a
file named Upload.cs.

I want to pass Upload.cs a Session variable (username) that is set in
default.aspx.

Setting up a session variable in default.aspx is no problem, but how
do I make it available to Upload.cs? I think it's a matter of
writting code into the following two files: Global.asax.cs, and
obviously, Upload.cs, but how exactly is it done?

I know this is a beginner question, and I've TRIED finding the answer,
but somehow it seems like it's harder to find answers to the most
basic questions.

May 16 '07 #3

A couple of basic things, to add to the others.

When you put something in (or take it out), it is of type "object".

So when you put in an EmpID (int) of "1001" .... it goes in as an object.

When you pull it out, you have to remember its an object, and then
cast/convert it to the proper type you want.

IN:

int empid = 1001;
HttpContext.Current.Session["MyKey"] = empid;
OUT

int foundEmpId = Convert.ToInt32 (
HttpContext.Current.Session["MyKey"] );

or better

int foundEmpId = 0 ;
if(null != HttpContext.Current.Session["MyKey"] )
{
foundEmpId = Convert.ToInt32 (
HttpContext.Current.Session["MyKey"] );
}

There you go. That should help.

"Dave" <on********@gmail.comwrote in message
news:11*********************@q75g2000hsh.googlegro ups.com...
I have a site with an App_Code folder that has Global.asax.cs and a
file named Upload.cs.

I want to pass Upload.cs a Session variable (username) that is set in
default.aspx.

Setting up a session variable in default.aspx is no problem, but how
do I make it available to Upload.cs? I think it's a matter of
writting code into the following two files: Global.asax.cs, and
obviously, Upload.cs, but how exactly is it done?

I know this is a beginner question, and I've TRIED finding the answer,
but somehow it seems like it's harder to find answers to the most
basic questions.

May 16 '07 #4
If you have a class that is not a "page", it would need to have a reference
to System.Web, and a "using System.Web" directive, and then you gain access
to the current HttpContext with

System.Web.HttpContext.Current.<Server, Session, Response... etc>
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Dave" wrote:
I have a site with an App_Code folder that has Global.asax.cs and a
file named Upload.cs.

I want to pass Upload.cs a Session variable (username) that is set in
default.aspx.

Setting up a session variable in default.aspx is no problem, but how
do I make it available to Upload.cs? I think it's a matter of
writting code into the following two files: Global.asax.cs, and
obviously, Upload.cs, but how exactly is it done?

I know this is a beginner question, and I've TRIED finding the answer,
but somehow it seems like it's harder to find answers to the most
basic questions.

May 16 '07 #5
On May 16, 6:19 pm, Dave <onlinef...@gmail.comwrote:
I have a site with an App_Code folder that has Global.asax.cs and a
file named Upload.cs.

I want to pass Upload.cs a Session variable (username) that is set in
default.aspx.

Setting up a session variable in default.aspx is no problem, but how
do I make it available to Upload.cs? I think it's a matter of
writting code into the following two files: Global.asax.cs, and
obviously, Upload.cs, but how exactly is it done?

I know this is a beginner question, and I've TRIED finding the answer,
but somehow it seems like it's harder to find answers to the most
basic questions.
The Session() value persists in the user's session and can be accessed
in any ASP.NET page within an application.

'Assign a value to the myvariable session variable.
Session("myvariable") = "somevalue"

'Retrieve the value of the myvariable session variable.
If not(Session("myvariable")is nothing) Then
Dim myString As String = Session("myvariable").ToString()
End If

http://support.microsoft.com/kb/307598

May 16 '07 #6
On May 16, 11:43 am, "Mark Rae" <m...@markNOSPAMrae.netwrote:
"Dave" <onlinef...@gmail.comwrote in message

news:11*********************@q75g2000hsh.googlegro ups.com...
Setting up a session variable in default.aspx is no problem, but how
do I make it available to Upload.cs?

You don't need to pass Session variables around - they are available to the
entire Session anyway...

string strVariable = HttpContext.Current.Session["MyVariable"].ToString();

--http://www.markrae.net

Then, my problem must be different, because it doesn't work, and I've
been getting this error in my application log:

"Object reference not set to an instance of an object"

Is this possibly related to the class inside upload.cs that this is
being called from?

public void ProcessRequest(HttpContext context)
{
string strVariable =
HttpContext.Current.Session["username"].ToString();
}

If so, what can I do?

May 16 '07 #7
"Dave" <on********@gmail.comwrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...
public void ProcessRequest(HttpContext context)
Ah - is this an HttpHandler, then...?
If so, what can I do?
It's a little diffcult without knowing what you're doing, but does this
work...?

string strVariable = context["username"].ToString();

--
http://www.markrae.net

May 16 '07 #8
Sorry if this is a duplicate. I'm not seeing my responses on this
end:

I do already have "using System.Web;" at the top of the upload.cs
file, but I keep ketting the object reference error.

Maybe it has something to do with #region?

If it helps, here is the entire upload.cs file:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Web.Services;

public class Upload : IHttpHandler
{
public Upload()
{
}
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}

public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count 0)
{
string tempFile = context.Request.PhysicalApplicationPath;
for(int j = 0; j < context.Request.Files.Count; j++)
{
HttpPostedFile uploadFile = context.Request.Files[j];
if (uploadFile.ContentLength 0)
{
if(null != HttpContext.Current.Session["username"])
{
string strVariable =
HttpContext.Current.Session["username"].ToString();
}
uploadFile.SaveAs(string.Format("{0}{1}{2}",
tempFile, "Upload\\" , uploadFile.FileName));

}
}
}
HttpContext.Current.Response.Write(" ");
}
#endregion
}
May 16 '07 #9

Put null checks around EVERYTHING to figure this one out.
I quickly saw missing

if (null != context )
{

}
else
{
Console.WriteLine ("Missing context");
}

if (null != uploadFile )
{

}
else
{
Console.WriteLine ("Missing uploadFile ");
}

and you need the scope of strVariable to be outside the {} it is in.
im also removing the hungarian notation

string varValue = string.Empty;
if(null != HttpContext.Current.Session["username"])
{
varValue = HttpContext.Current.Session["username"].ToString();
}
else
{
Console.WriteLine ("Missing username session variable");
}

if (varValue.Length>0)
{
Console.WriteLine( varValue ) ;
}

Do that and you'll find it.

"Dave" <on********@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
Sorry if this is a duplicate. I'm not seeing my responses on this
end:

I do already have "using System.Web;" at the top of the upload.cs
file, but I keep ketting the object reference error.

Maybe it has something to do with #region?

If it helps, here is the entire upload.cs file:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Web.Services;

public class Upload : IHttpHandler
{
public Upload()
{
}
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}

public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count 0)
{
string tempFile = context.Request.PhysicalApplicationPath;
for(int j = 0; j < context.Request.Files.Count; j++)
{
HttpPostedFile uploadFile = context.Request.Files[j];
if (uploadFile.ContentLength 0)
{
if(null != HttpContext.Current.Session["username"])
{
string strVariable =
HttpContext.Current.Session["username"].ToString();
}
uploadFile.SaveAs(string.Format("{0}{1}{2}",
tempFile, "Upload\\" , uploadFile.FileName));

}
}
}
HttpContext.Current.Response.Write(" ");
}
#endregion
}


May 16 '07 #10
Dave,
You need to implement IRequiresSessionState interface with this.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Dave" wrote:
Sorry if this is a duplicate. I'm not seeing my responses on this
end:

I do already have "using System.Web;" at the top of the upload.cs
file, but I keep ketting the object reference error.

Maybe it has something to do with #region?

If it helps, here is the entire upload.cs file:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Web.Services;

public class Upload : IHttpHandler
{
public Upload()
{
}
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}

public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count 0)
{
string tempFile = context.Request.PhysicalApplicationPath;
for(int j = 0; j < context.Request.Files.Count; j++)
{
HttpPostedFile uploadFile = context.Request.Files[j];
if (uploadFile.ContentLength 0)
{
if(null != HttpContext.Current.Session["username"])
{
string strVariable =
HttpContext.Current.Session["username"].ToString();
}
uploadFile.SaveAs(string.Format("{0}{1}{2}",
tempFile, "Upload\\" , uploadFile.FileName));

}
}
}
HttpContext.Current.Response.Write(" ");
}
#endregion
}
May 16 '07 #11
On May 16, 3:10 pm, "sloan" <s...@ipass.netwrote:
Put null checks around EVERYTHING to figure this one out.

I quickly saw missing

if (null != context )
{

}

else
{
Console.WriteLine ("Missing context");

}

if (null != uploadFile )
{

}

else
{
Console.WriteLine ("Missing uploadFile ");

}

and you need the scope of strVariable to be outside the {} it is in.
im also removing the hungarian notation

string varValue = string.Empty;
if(null != HttpContext.Current.Session["username"])
{
varValue = HttpContext.Current.Session["username"].ToString();
}
else
{
Console.WriteLine ("Missing username session variable");

}

if (varValue.Length>0)
{
Console.WriteLine( varValue ) ;

}

Do that and you'll find it.

"Dave" <onlinef...@gmail.comwrote in message

news:11**********************@l77g2000hsb.googlegr oups.com...
Sorry if this is a duplicate. I'm not seeing my responses on this
end:
I do already have "using System.Web;" at the top of the upload.cs
file, but I keep ketting the object reference error.
Maybe it has something to do with #region?
If it helps, here is the entire upload.cs file:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Web.Services;
public class Upload : IHttpHandler
{
public Upload()
{
}
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count 0)
{
string tempFile = context.Request.PhysicalApplicationPath;
for(int j = 0; j < context.Request.Files.Count; j++)
{
HttpPostedFile uploadFile = context.Request.Files[j];
if (uploadFile.ContentLength 0)
{
if(null != HttpContext.Current.Session["username"])
{
string strVariable =
HttpContext.Current.Session["username"].ToString();
}
uploadFile.SaveAs(string.Format("{0}{1}{2}",
tempFile, "Upload\\" , uploadFile.FileName));
}
}
}
HttpContext.Current.Response.Write(" ");
}
#endregion
}- Hide quoted text -

- Show quoted text -
Does Console.Writeline work without Visual Studio? That would be
nice. Anyway, I decided to use query string variables instaed and
it's working nicely now. I think another program (flash uploader)
might have been getting in the way. Thanks.

May 17 '07 #12

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...
0
by: Fie Fie Niles | last post by:
I posted a message yesterday in regard to session variable error message. Do you know what Microsoft DLL that processes Session variable? On that server, we can not seem to register anything....
7
by: Billy Jacobs | last post by:
I am having a problem with my session variable being set to Null for no apparent reason. I am declaring it like the following when the user logs in. dim objUserInfo as new clsUserInfo 'Set...
5
by: joled | last post by:
Hello, It looks like a session variable that I created in the global.asa is being reset by the global.asa when you return to a pge in the virtual root directory. Ex. /vroot/index.asp - website...
3
by: Enoch Chan | last post by:
I would like to set a Session variable to a value. In Vbscript it should be Session("ZoomValue")=500 How can I set this session variable by using Javascript? Thanks
14
by: Bobby | last post by:
I have this line, at this time the value of Session("Login") is 1: Response.Write "<a href=" & Request("script_name") & _ "?action=show&o=" & rsCustOrders("OrderID") & _ ">" &...
2
by: George | last post by:
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 == "something" When I...
3
by: vvenk | last post by:
Hello: This was posted in a wrong forum; that's why I am posting here again. Sorry. I just wrote my first ASP.Net application, tested it on my development environment and deployed it on a...
5
by: Twayne | last post by:
Hi, If ever a newbie wants to know how much he has to learn yet, he only has to look here<g>!! ANYway: PHP 5.2.5; XP Pro SP2+, local Apache Server My actual question is: How do I get a...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.