Global variable of ASP.net

Global variables are those variables that can be accessed anywhere in the application. Global variable should always be used with caution. It stores data. The most common way of accessing global variables in ASP.net are by using Application, Cache, and Session Objects.

ApplicationApplication objects are application level global variables, that need to be shared for all user sessions. Thus, data specific to a user should’nt be saved in application objects. While using application objects, the objects are locked so that multiple page requests cannot access a specific application object. Below is a code example for usage of application object…

Application.Lock(); Application("UserData") = "dotnetuncle"; Application.UnLock(); Response.Redirect("DestinationPage.aspx"); //DestinationPage.aspx gets the value from the Application State String sString = Application("UserData").ToString(); 

CacheThe cache object is similar to the application object in scope, however, it does not need any explicit locking and unlocking. Code below shows usage of Cache object…

Cache("Userdata") = "dotnetuncle"; Response.Redirect("DestinationPage.aspx"); //Destination.aspx retrieves the value from Cache object String sString = Cache("Userdate").ToString(); 

The cache object also shares data across all user sessions. The cache object has features like it can automatically expire cached content after specified time periods or once memory consumption has reached a maximum.

Session – The session object is used to store the data specific to a user for the entire length of a user’s visit to a website. Below is a code that shows usage of the session object in ASP.NET …

//InitialPage.aspx stores the user’s credentials in Session state Session("UserName") = txtUserName.Text; Server.Transfer("DestinationPage.aspx"); //DestinationPage.aspx gets the user’s name from Session state String sString = Session("UserName").ToString(); 

ASP.NET stores session values in the server memory. If there are plenty of active user’s of a website, then the memory consumption on the server increases by leaps. Because of this reason, large websites use very less Session Variables. Session state can be configured to be automatically stored in a SQL Server database, or it can be configured to be stored centrally in a state server within a server farm. By default, a user’s session ends 20 minutes after their last page request and their data goes out of scope, freeing it from memory. In case user information is to be tracked by a large website, then a oookie is preferred.

Cookie – A cookie is a piece of data that is stored on a user’s browser. Thus, a cookie does not use any server memory.

Categories: ASP.NET , C#

Updated: March 01, 2010

You May Also Enjoy

Utilizing Bulk Operations with Entity Framework and ASP.NET Core

Bulk operations often serve as pivotal performance enhancers within applications. This article delves into the effective utilization of bulk operations using.

Optimistic Concurrency Management in ASP.NET Core Application Using Raw SQL and ADO.NET

Effectively dealing with concurrency is crucial, especially in large business applications, and it plays a significant role in financial systems. This articl.

Step-by-Step Guide to Integrating Audit Trail in ASP.NET Core Using ADO.NET and Raw SQL

The Audit Trail stands out as a critical feature in enterprise applications, primarily serving essential roles in security and fraud transaction detection. T.

Optimistic Concurrency Management in ASP.NET Core Application Using Hypermedia

Optimistic concurrency is a common method for handling such situations. In this article, I’ll show a simple way to manage concurrency in ASP.NET using hyperm.