View Full Version : Web Page Global Variables

02-08-2005, 08:10 AM
I just learned that using Global Variables is a VERY BAD IDEA in Web Page application because it tends to cause issues when multiple users are on the page at the same time accessing
and trying to use that particular variable.

I was instructured to use one of the following:
- use a cookie
- use Session["var"]
- just use a class variable in your page code!!
- (with possible Server.Transfer)": use Context["var"]
- use a ViewState["var"] variable or a hidden TextBox
- use Application["var"]

Therefore the "page class member variable"
However I am really unfamilar with these nor do I know which is the best to use in my situation.

Currently my web application have a calendar, users can connect and select a date for a task to run, so I could have many users scheduling various tasks at the same time.
The DateTime needs to be saved, currently I was using Global Variables as defined previouslly (which is supposdly BAD).

Can someone tell me which method I should be using (and possible why) and maybe an example on how I could apply this to me DateTime varable (dtSelected).


02-08-2005, 12:49 PM
I think if you are talking about storing data Permanently or for any extended period of time you should consider Database Methods.

Thing is Application variables are open for all users on your site to see its contents. For example I used application variables to keep a track of which users were on my site on a particular time.

Session variables on the hand are created independent of any other session for each user that logs on to your site. When a user requests your site from the server, the server creates a session specifically for that user. Each session has a timeout and it gets destroys after the timeout has elasped. This is generally used to store SMALL amounts of data and it is global in the sense that the variable can be accessed from whatever the user is on given the timeout has not elasped.

The other methods I don't like or are not familiar with. Server.execute just pauses processes of one page.... processess another in the parentheses and then resumes processing of the main page. Don't see how that is even relevant. Cookies you could maybe do some reading on but I don't know too much to comment about that but my suggestion is this:

If you are thinking of storing data, and if you are thinking of going larger scale at any point in time in terms of your site. You're gonna need physical storage, because you cannot rely on any of the variables i mentioned earlier to store any large amounts of data. Possibly cookies might be a interesting solution and you could do some reading but I SUGGEST DATABASE!!!!


02-09-2005, 01:50 PM
I often use session variables for things like storing what the user entered (if anything) for search fields on a form.

Session("CustomerDialog-LName") = Request.Form("LName")
Session("CustomerDialog-CompanyName") = Request.Form("CompanyName")

And then on the page to show the data:

' Set strSQL.
strSQL = "SELECT * FROM tblCustomer WHERE (1=1)"
If Session("CustomerDialog-LName") <> "" Then
strSQL = strSQL & " AND (Rep='" & Session("CustomerDialog-LName") & "')"
End If
If Session("CustomerDialog-CompanyName") <> "" Then
strSQL = strSQL & " AND (Customer='" & Session("CustomerDialog-CompanyName") & "')"
End If

02-09-2005, 04:21 PM
that can be useful for securely storing data if you have a form continuing on multiple pages but make sure you wipe teh sessions as much as possible and dont set the timeout too high and do not store a lot of data on it, im with jaywhy on this one, a database sounds like its really what you are looking for