Global Session Variable

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

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




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.
 
Back
Top