By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,715 Members | 1,773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,715 IT Pros & Developers. It's quick & easy.

How to access Session,Application & QueryString variables in a class file.

100+
P: 111
Here I will given an example on how to access the session, application and querystring variables in an .cs class file.

Using System.Web.HttpContext class.

1) For accesing session variables : System.Web.HttpContext.Current.Session[index]
2) For accesing Application variables : System.Web.HttpContext.Current.Application[index]
3) For accesing QueryString variables : System.Web.HttpContext.Current.Request.QueryString[index]

Here is a simple example where Startup page is webform2.aspx in which webform1.aspx is invoked in which session application and querystring variables are initialized/assigned.

Example :
------------
1) Code in webform2.aspx:
------------------------------------------
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Example
{
/// <summary>
/// Summary description for WebForm2.
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Server.Transfer("webform1.aspx?QVar='Ashok'"); }

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

2) Code in webform1.aspx
---------------------------------------
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


namespace ExampleBravaViewer
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
private void Page_Load(object sender, System.EventArgs e)
{
string str= "Bharath Reddy VasiReddy";
Session["Sess"] = str;
Application["App"] = "eXensys Software Solutions Ltd";
ExampleNamespace.ExampleSort.SortingFunct();

}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

3)Code in Class File
--------------------------------------
using System;
namespace ExampleNamespace
{
/// <summary>
/// Summary description for Exampl.
/// </summary>
public class ExampleSort
{
public ExampleSort()
{
//
// TODO: Add constructor logic here
//
}

public static void SortingFunct()
{
string str = (string)System.Web.HttpContext.Current.Session["Sess"];
//This will fetch Bharath Reddy VasiReddy into str string variable.
string strServer = (string)System.Web.HttpContext.Current.Request.Que ryString["QVar"];
//This will fetch Ashok into strServer variable.
string strAppl = (string)System.Web.HttpContext.Current.Application["App"];
// This will fetch 'eXensys Software Solutions Ltd' into strAppl string variable
}
}
}


Thanks & Regs
Bharath Reddy VasiReddy
Jun 18 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.