[aspx.cs] Global var for the application

O

oliv

Hi,
new to .NET, I need some unique var among the whole application in
during the whole lifecycle of the webapp.

This var is a Boolean :

at start time of the webapp, I want this var to be set as false

I did that through a file Global.asax

<%@ Application Language="C#" %>

<script runat="server">

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup --
Application["isStarted"] = false;
....


Is that the proper way ?


Then I use this var among the different pages. Some instance of the
page can read or change the value of this var.

My problem, is that after some while runing , the var seems to be
removed from the Application and I get this error message


Server Error in '/' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:



Line 43: else if ((((Boolean)Application["isStarted"]) ==
false)



thanks for help
 
O

oliv

For some reason, my Global.asax might not have been executed.
I will thus rephrase my question :

If I just created and set a var this way from a page:
Application["isStarted"]=true;

what will be the lifecycle of this var "isStarted" ? will it be
accessible by any other instance of all pages during the whole life of
the application ?

thanks
 
A

Aidy

That could should work if the global.asax file is in the root of the web
folder.

The application will be global and accessable by all users using the site.
It will live as long as the Application lives, which is while people are
still browsing the site.

The alternative is to use a static variable on a configuration class. The
good thing about doing that is that it is strongly typed, unlike using the
Application object.
 
O

oliv

How would you explain that I set var this way
Application["isStarted"]=true;

I use it this way
if ((((Boolean)Application["isStarted"])
and it works


but after some time of inactivity of the web site (the app was not
shut down and restarted), if i call that code again I get
Exception Details: System.NullReferenceException: Object reference
not
set to an instance of an object.

Is that because there is no Global.asax ? why has the var vanished ?
 
G

George Ter-Saakov

My favorite way to do those type of things.

public class clsGlobal
{
static public bool _isStarted = false;
static public string _sConnection;
}

Everywhere in the code
if( clsGlobal._isStarted )
con.Open(clsGlobal._sConnection);

........

But be careful if you want to change those variables since ASP.NET is
multithreaded environment and you need to obtain lock before you should be
changing "global" variables.

George.
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top