Thursday, March 22, 2012

Session Variables

Working with ASP.NET 1.1 - C#

I have been attempting to locate all of reading and writing of session
variables into a class in its own file. The file contains:
using System;
using System.Collections;
using System.Web;
using System.Web.SessionState;
using System.Xml;
using System.Text;
using Microsoft.Win32;
namespace SessionVariablesClasses
{
public class SessionVariablesClass
{
I am getting the following error message:
c:\inetpub\wwwroot\OB_EEG_WhatIf\SessionVariables. cs(84): The name 'Session'
does not exist in the class or namespace
'SessionVariablesClasses.SessionVariablesClass'

on the following line of code:
try
{
_bnUserHasBeenValidated = Session["UserHasBeenValidated"].ToString();
}
catch
{
_bnUserHasBeenValidated = false;
}You can find the Session Object in System.Web namespace

You could get at the session like:
System.Web.HttpContext.Current.Session["MyVar"] = "Blah";

HTH,
Tony

"Robert E. Flaherty" <Bobf@.TimeVision.Com> wrote in message
news:eTGd%23gjSGHA.4608@.tk2msftngp13.phx.gbl...
> Working with ASP.NET 1.1 - C#
> I have been attempting to locate all of reading and writing of session
> variables into a class in its own file. The file contains:
> using System;
> using System.Collections;
> using System.Web;
> using System.Web.SessionState;
> using System.Xml;
> using System.Text;
> using Microsoft.Win32;
> namespace SessionVariablesClasses
> {
> public class SessionVariablesClass
> {
> I am getting the following error message:
> c:\inetpub\wwwroot\OB_EEG_WhatIf\SessionVariables. cs(84): The name
> 'Session' does not exist in the class or namespace
> 'SessionVariablesClasses.SessionVariablesClass'
> on the following line of code:
> try
> {
> _bnUserHasBeenValidated = Session["UserHasBeenValidated"].ToString();
> }
> catch
> {
> _bnUserHasBeenValidated = false;
> }
When you reference Session within a Page, you are resolving a property of
the page object. When referencing Session within a class other than a page,
you will need to qualify it as follows:

HttpContext.Current.Session["MySessionVariableName"] = "abc";

Also, you really shouldn't be using a try / catch to check for a valid
Session variable. Exceptions are expensive. Instead, try testing your
session variable for null:

if(Session["UserHasBeenValidated"] != null)
{
_bnUserHasBeenValidated = true;
}
else
{
_bnUserHasBeenValidated = false;
}

Hope this helps you out.

--
Andrew Robinson
http://blog.binaryocean.com

"Robert E. Flaherty" <Bobf@.TimeVision.Com> wrote in message
news:eTGd%23gjSGHA.4608@.tk2msftngp13.phx.gbl...
> Working with ASP.NET 1.1 - C#
> I have been attempting to locate all of reading and writing of session
> variables into a class in its own file. The file contains:
> using System;
> using System.Collections;
> using System.Web;
> using System.Web.SessionState;
> using System.Xml;
> using System.Text;
> using Microsoft.Win32;
> namespace SessionVariablesClasses
> {
> public class SessionVariablesClass
> {
> I am getting the following error message:
> c:\inetpub\wwwroot\OB_EEG_WhatIf\SessionVariables. cs(84): The name
> 'Session' does not exist in the class or namespace
> 'SessionVariablesClasses.SessionVariablesClass'
> on the following line of code:
> try
> {
> _bnUserHasBeenValidated = Session["UserHasBeenValidated"].ToString();
> }
> catch
> {
> _bnUserHasBeenValidated = false;
> }
I have a well encapsulated Session object wrapper at my blog:

http://spaces.msn.com/sholliday/ 10/24/2005 entry

it would be a trivial exercise to add a .ClearAll() method to it, since
I have all the objects in a collection member variable.

Robert E. Flaherty wrote:
> Working with ASP.NET 1.1 - C#
> I have been attempting to locate all of reading and writing of session
> variables into a class in its own file. The file contains:
> using System;
> using System.Collections;
> using System.Web;
> using System.Web.SessionState;
> using System.Xml;
> using System.Text;
> using Microsoft.Win32;
> namespace SessionVariablesClasses
> {
> public class SessionVariablesClass
> {
> I am getting the following error message:
> c:\inetpub\wwwroot\OB_EEG_WhatIf\SessionVariables. cs(84): The name 'Session'
> does not exist in the class or namespace
> 'SessionVariablesClasses.SessionVariablesClass'
> on the following line of code:
> try
> {
> _bnUserHasBeenValidated = Session["UserHasBeenValidated"].ToString();
> }
> catch
> {
> _bnUserHasBeenValidated = false;
> }
> Also, you really shouldn't be using a try / catch to check for a valid
> Session variable. Exceptions are expensive.

depends. see http://www.yoda.arachsys.com/csharp/exceptions.html

> Instead, try testing your session
> variable for null:
> if(Session["UserHasBeenValidated"] != null)
> {
> _bnUserHasBeenValidated = true;
> }
> else
> {
> _bnUserHasBeenValidated = false;
> }

but yes, this would be better.

Hans Kesting

0 comments:

Post a Comment