Session Variables

R

Robert E. Flaherty

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;
}
 
A

Anthony Merante

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
 
A

Andrew Robinson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top