473,320 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

[aspx.cs] Global var for the application

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

Apr 19 '07 #1
4 1508
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

Apr 19 '07 #2
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.

<ol**@linuxmail.orgwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
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

Apr 19 '07 #3
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 ?

Apr 19 '07 #4
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.

<ol**@linuxmail.orgwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
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

Apr 19 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David McCormack | last post by:
I have a small project that I'm writing to help me learn C# and the .Net Framework. This project is a WinForms program with multiple forms that accesses MSDE. I've got most of it done but I've hit...
7
by: Tamir Khason | last post by:
I need the way to interrupt (freeze) ANY program currenlty running at the computer, then to activate the application and return the previous applications state after my application execution. Is...
5
by: Dennis C. Drumm | last post by:
Is there one place (local xml file, registry, etc.) that all user can read and write to?? I have some settings that applicable to all users, but when a restricted rights user start my...
6
by: Craig Traxler | last post by:
I would like to create an instance of Windows Media Player that I can manipulate from within ASP.NET pages. Do I just create an instance of Windows Media Player from within the Application_OnStart...
3
by: Gort | last post by:
Hello I'm starting to do a little application for fun with VB.NET and would like to store some information about the user (name, point already won), loaded from a Xml file after login...
1
by: robear | last post by:
Hi All. I have an application that has started to give me "memory cannot be read" errors. This app uses several threads and I suspect the thread has closed, but the garbage collection is hitting...
1
by: tool | last post by:
Hi all, I Followed these steps : 1. Added a .master file with 3 contentplaceholder controls. 2. Added an aspx file with the @Page directive parameter 'MasterPageFile' set to my .master file....
1
by: Matt | last post by:
Hi, I am writing a web appplication that will use a global licensing paradigm. For every instance of the server I want to check the licensing requirements i.e the application in a global...
2
by: Jesse Aufiero | last post by:
I'm using the built-in global UnhandledException event to handle errors in my application, but when I call e.ExitApplication = True to kill the application, the application disappears, but the...
2
by: The Great Pawn Hunter | last post by:
Hi, We have an old .asp application. We want to write a new .aspx application to replace it. However, many other programs still reference the old .asp. What I want to do is have the .aspx run...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.