all groups > vj# > march 2004 >
You're in the

vj#

group:

HOW TO: Syntex for getting or setting a Session Variable within a Class


HOW TO: Syntex for getting or setting a Session Variable within a Class Burton Wilkins
3/10/2004 7:56:10 AM
vj#: Dear Authorities

I need some help with syntax. I have a JSharp Web Application project which has some classes within them. In one, I would like to get and set an ASP.Net Session variable

At http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebsessionstatehttpsessionstateclassitemtopic1.asp,
MSDN advises that to set or get a Session variable one would use

returnValue = HttpSessionStateObject.Item(name)
HttpSessionStateObject.Item(name) = returnValue
-or
returnValue = HttpSessionStateObject(name)
HttpSessionStateObject(name) = returnValue

using the namespace I presume

import System.Web.SessionState.*;

However, I have not been able to get these suggested syntaxes to work

(1) intTest = HttpSessionStateObject("ds")
(2) HttpSessionStateObject("ds") = intTest

Both produces the error: Cannot find method "HttpSessionStateObject(String) in "JSharpTest.Class1"

(2) intTest = HttpSessionStateObject.Item("ds")
(4) HttpSessionStateObject.Item("ds") = intTest

Both produces the error: Name "HttpSessionStateObject" is not defined in current context

Could you please advise what would be a workable syntax to get or set an ASP.Net Session varable from with a Class

Thank you for your response in advance to this question

Burton G. Wilkin



Re: HOW TO: Syntex for getting or setting a Session Variable within a Class Lars-Inge Tønnessen
3/10/2004 5:32:59 PM
Hello Burton,

You must first get the current http context. You do that by:
System.Web.HttpContext context = System.Web.HttpContext.get_Current();

Get the session state with:
System.Web.SessionState.HttpSessionState state = context.get_Session();

Now you can eg. set the timeout etc..
state.set_Timeout( 15 );

Add two variables:
state.Add("One", new String("One Hello") );
state.Add("Two", new String("Two here") )


If you want to read the variable:
System.Web.HttpContext context = System.Web.HttpContext.get_Current();
System.Web.SessionState.HttpSessionState state = context.get_Session();
this.Label1.set_Text( ""+state.get_Item("Two") );


Example:
Lets say you have an ASP.Net page with a label called "label1" and a button
called "Button1" with a click handler.

import System.Collections.*;
import System.ComponentModel.*;
import System.Data.*;
import System.Drawing.*;
import System.Web.*;
import System.Web.SessionState.*;
import System.Web.UI.*;
import System.Web.UI.WebControls.*;
import System.Web.UI.HtmlControls.*;
/**
* Summary description for WebForm1.
*/
public class WebForm1 extends System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
private System.Web.HttpContext context;
private System.Web.SessionState.HttpSessionState state;

private void Page_Load(Object sender, System.EventArgs e)
{
// Put user code to initialize the page here
context = System.Web.HttpContext.get_Current();
state = context.get_Session();
state.set_Timeout( 15 ); // Holds a state for 15 minutes.
state.Add("Value", new String("Hello") );
state.Add("Two", new String("Two here") );
}

#region Web Form Designer generated code
protected void OnInit(System.EventArgs e)
{
InitializeComponent();
super.OnInit(e);
}

/**
* Required method for Designer support - do not modify
* the contents of this method with the code editor.
*/
private void InitializeComponent()
{
this.Button1.add_Click( new
System.EventHandler(this.Button1_Click) );
this.add_Load( new System.EventHandler(this.Page_Load) );
}
#endregion

private void Button1_Click (Object sender, System.EventArgs e)
{
System.Web.HttpContext context =
System.Web.HttpContext.get_Current();
System.Web.SessionState.HttpSessionState state =
context.get_Session();
this.Label1.set_Text( ""+state.get_Item("Two") );
}
}




Lars-Inge Tønnessen
www.larsinge.com

AddThis Social Bookmark Button