Saturday, March 24, 2012

What is the best way to acces global variables?

Hello Everyone,
I am building a web application with about 10 pages. Nearly every page uses the same exact SqlConnection variable (as well as many other variables). What is the best way to store variables that are accessed across every page?
I have seen website use Application["variable"] = string Yada; inside of the Application_Start on the global file, but for as many of those suggestions i have seen people saying that storing variables inside of Application[" "] can severly slow down your website or even cause it to crash.
I suppose you could also do this sort of thing inside of a code behind file
using System;
using System.Web.UI;

public class GlobalVariables2 : Page{
public string strHello = "hello";

}
But is that any better?

What is the best way to store and access global variables?
Thanks

Web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="server=(local);Trusted_Connection=true;database=RHITS;"/>
</appSettings>
<system.web>
....

Using ConfigurationSettings.AppSettings["ConnectionString"] ,you can get the variables in the web.config file.
the connection string was just an example.. i have about 15 others that are also used
Using the web.config file is a good method you can store multiple keys and call them as shown below here is an example:

<appSettings>

<!-- CONNECTION STRING - Sets up the database connection string. -->

<addkey="ConnectionString"value="connectionstringhere"/>

<!-- XML Directory. -->

<addkey="XML"value="C:\MyWebsite\secure\xml\"/>

<!-- SMTP SERVER - Server to send mail out through. -->

<addkey="smtpserver"value="217.217.217.217"/>
</appSettings>

Once this is done you can then just call them on any page of you web application using:

Dim strSMTPServerAsString = ConfigurationSettings.AppSettings("smtpserver")
Dim strXMLDirAsString = ConfigurationSettings.AppSettings("XML")

Hope this is of help to you..
Kind Regards,
Jon Sharratt


If you want to store variables that can be accessed globally between different pages, store them in Session. Application is cleared out after every 5000 page requests, so that will cause unpredictable behavior.


mugsywwiii wrote:

If you want to store variables that can be accessed globally between different pages, store them in Session. Application is cleared out after every 5000 page requests, so that will cause unpredictable behavior.

Putting global variables into Session makes no sense at all.
Primalfear, I recommend that you follow jsharratt's advice of using the appSettings section of Web.config -- that's what it's there for.

Hi,

I am not that good in c# however I use static variables:

public class CGlobal {
public static string strVariable="Hello" ;
}
Then when I need the value somewhere I just type:
string strExample = CGlobal.strVariable ;
Thx,
Teddy


Depending on how much you worry about security, I wouldnt store my connection strings in the web.config. This file is viewable by anybody (until VS 2005 which then you can encrypt your web.config). I would recommend putting anything your not worried about in the web.config (xml paths, etc) but putting the conn string either in a file encrypted, or in the registry encrypted. I use the registry.
Nick


If you are worried about security I encrypt my connection string and any passwords with TripleDES and put them in the web config as encrypted data. I then just Decrypt in the string data stored in the web.config when i need them throughout the applicaiton, recently I posted about using TripleDES here. This may help with security for you if you are interested.

http://forums.asp.net/937243/ShowPost.aspx

Kind Regards,
Jon Sharratt

SomeNewKid wrote:

mugsywwiii wrote:

If you want to store variables that can be accessed globally between different pages, store them in Session. Application is cleared out after every 5000 page requests, so that will cause unpredictable behavior.

Putting global variables into Session makes no sense at all.
Primalfear, I recommend that you follow jsharratt's advice of using the appSettings section of Web.config -- that's what it's there for.


Eh, I didn't read the part about connection strings. He did sayvariables though (and the code sample he provided used a variable), what he really meant wasconstants.

Ah.. perhaps i did mean constants and not variables..

Sorry


Hi! I have the same question.
The answers I saw are not really what seems a nice solution to me because it is too much code.
I want to have some constants which are available through out the whole application for all users. I need them very frequently in all pages so the code wouldn't be very readable if I have to use strings like "ConfigurationSettings.AppSettings("pathImages")". I just want to call them through a name I gave them, in this case "pathImages".
Before .net I just created a page with my constants and included it in all other pages. So I could use these constants from everywhere just through a short descriptive name. I'm new to asp.net and try to do the things like it is recomended.
Markus
You can always create a seperate class with a bunch of public or public shared variables, or can even use read only properties for constants.
smtraber

0 comments:

Post a Comment