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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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.
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.


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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

777 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