472,125 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,125 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 11371
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Billy Jacobs | last post: by
3 posts views Thread by Enoch Chan | last post: by
2 posts views Thread by George | last post: by
3 posts views Thread by vvenk | last post: by
5 posts views Thread by Twayne | last post: by
reply views Thread by leo001 | last post: by

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.