Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


ASP.NET variable scope

Posted on 2003-03-20
Medium Priority
Last Modified: 2010-08-05
Does anybody know how I can use variables in the whole ASP.NET program? page_load seams to clear the data stored in the variables. I have an array with data in it, and it has to keep all it's information.

Question by:paing0d

Expert Comment

ID: 8214907
Declare variable in Global.asax in the Application_OnStart() block. That's if the variable is the same for all users.

If different users hold their own variables for the session then you need to use session state.

The following for more info:


A little more information and I may be able to give you some code

Author Comment

ID: 8216217
Let me explain my webprogram.
First users have to login.
When the succesfully logged in they should answer bout 15 questions. When done they should be able to click the next button, and then there are 15 more questions etc. The can answer those questions with 4 possibilitys. Like easy, moderate, hard. A variable needs to be decleared to store the users checkboxes.value for each tab. So you would have an array like:

array(5,15) as integer 'x='tab' number y=question number

if a certain answer is checked then there should be a number in the array. So if the first question is answered with easy then array(1,1) = 1 if it's moderate then array(1,1) = 2 etc

The questions are in a textfile. I fill an array with all the information in page_load (don't think that's a good idea though).

I made a variable page_number. This is the current page number. So i can easily set the new questions when the next or previous buttons is pressed.

After completing all the questions, a new part is made visible. The questions part will be made invisible. When you've finished the last part the results should be mailed to a number of persons.

ps. Login part is also made invisible after a succesfull login. Then Question part will be made visible. (put those 3 parts on panels so it's easily to put it invisible).


Expert Comment

ID: 8221680
OK! Thanks, that helps.

It looks like the simplest way to handle this is to use multiple panels on one page to handle the different sections of the form. This way you can handle all the data within a single form and can be handled by a single submit button and data processing routine.

These two tutorials should help:


Hope that solves the problem.
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.


Author Comment

ID: 8255503
Sorry for the late reaction. Was very busy for the last couple of days. I don't get it :( When i declare something and when i want to use it, it get clears by the page_load
Public variables don't seem to store the data well. Found a solution but that's not the right solution. Application("array") = array works very well, but it's not a multiple users system. Session("aray") = array doesn't work at all, and me.viewstate("array") = array doesn't work either. Can someone help me please. Tell me what to do. Need to store variables that work in the whole program

Thanks in advance,

Expert Comment

ID: 8256294

If you've experimented with Session, Application & ViewState and are still not satisfied, perhaps you should look at using a small database or XML file.

What constraints do you have @ your web host?

Author Comment

ID: 8259470
I don't know but i try to find it out.
So the only option is to save files to the server?

Expert Comment

ID: 8264236
I might be able to say better if you actually made a code sample available so I can see exactly how you need to use the stored data.

And you haven't made it clear whether you are concerned about losing the data for the questions on each page_load, or the answers...

If you make this clear, and provide a sample of what you are doing I will be able to post code to help you...

Author Comment

ID: 9548079
cookieless="true" solves the whole problem :p
if you don't know where that is then open the Web.config and there should be something called
<sessionState ....
cookieless="false" -> change it to cookieless="true"

thanks all :)
LVL 96

Expert Comment

by:Bob Learned
ID: 9688045
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Author Comment

ID: 9707769
I guess I solved my question myself :) What to do?


Accepted Solution

Lunchy earned 0 total points
ID: 9735296
PAQed, with refund (50)

Friendly Neighbourhood Community Support Admin

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Integration Management Part 2
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question