best way to store globally accessible values?

Last post 07-05-2009 4:28 PM by dotnetnoob. 12 replies.

Sort Posts:

  • best way to store globally accessible values?

    07-04-2009, 12:20 PM
    • Member
      52 point Member
    • dotnetnoob
    • Member since 11-17-2006, 4:08 PM
    • Posts 372

     Hi All: If I have values that I want to be able to call anywhere in my website, what is the best way to achieve this? For example if I have a common set of HTML color values that I want to be able to use on any page?

    In classic ASP I would either put them in an include file or maybe in the global file, but what is the best way to do this in vb.net?

  • Re: best way to store globally accessible values?

    07-04-2009, 1:18 PM
    • Member
      80 point Member
    • hminaya
    • Member since 10-17-2008, 3:04 AM
    • Dominican Republic
    • Posts 31
    • if they are user specific, then store them in  session variables
    • if they are NOT user specific, then store them in application variables
    Hector Minaya
    Microsoft Visual Basic MVP | Speaker INETA - Latam | MCSD | MCT | MCTS : SQL Server
    blogs: Hector Minaya | DotNetNuke SEO | Blogger SEO | facebook junkie | SEO Tools
  • Re: best way to store globally accessible values?

    07-04-2009, 2:08 PM
    • Member
      52 point Member
    • dotnetnoob
    • Member since 11-17-2006, 4:08 PM
    • Posts 372

    hminaya:
    • if they are user specific, then store them in  session variables
    • if they are NOT user specific, then store them in application variables

     

    Thanks for the quick reply...they would not be user specific...so do you suggest using global.asax, or is there a different way like storing them in appSettings? I've seen both but I'm not sure which is better?

    Additionally, I might want to be able to change these settings from an admin page. In the past, I would lock the application, refresh the settings, and then unlock it...is this still appropriate?

  • Re: best way to store globally accessible values?

    07-04-2009, 2:24 PM
    Answer

     Hi,

    You can maintain in session. As per my understanding you want to store a set of values. So  you can crate a class file with  get set methods.

    public abstract class BaseParameterPasser
    {
     private string url = string.Empty;

     public BaseParameterPasser()
     {
      if (HttpContext.Current != null)
       url = HttpContext.Current.Request.Url.ToString();
     }

     public BaseParameterPasser(string Url)
     {
      this.url = Url;
     }

     public virtual void PassParameters()
     {
      if (HttpContext.Current != null)
       HttpContext.Current.Response.Redirect(Url);
     }

     public string Url
     {
      get
      {
       return url;
      }
      set
      {
       url = value;
      }
     }

     public abstract string this[string name]
     {
      get;
      set;
     }

     public abstract ICollection Keys
     {
      get;
     }
    }

    Thanks :)

     

    Remember to click “Mark as Answer” on the post, if it helps you. Because It helps others to find the solution.

    Srinivas Kotra.


  • Re: best way to store globally accessible values?

    07-04-2009, 2:29 PM
    Answer
    • Participant
      989 point Participant
    • Scott927
    • Member since 09-20-2006, 4:53 AM
    • Phoenix, AZ
    • Posts 182

    In global.asax:

    public void Application_Start(object sender, EventArgs e)
    {
         Application["VariableName"] = "Your Value";
    }


    In your code-behind, you would call it like this:

    Response.Write(Application["VariableName"].ToString());

    Or, you could store them in web.config:

    <configuration>
         <appSettings>
              <add key="VariableName" value="Your Value" />
         </appSettings>
    </configuration>


    And access it in your code-behind like this:

    Response.Write(System.Configuration.ConfigurationManager.AppSettings["Variablename"].ToString());

    Of course, you could import System.Configuration and just call:

    Response.Write(ConfigurationManager.AppSettings["Variablename"].ToString());

    Scott M Schluer
    ---
    MCPD: ASP.NET Developer 3.5
  • Re: best way to store globally accessible values?

    07-04-2009, 2:30 PM
    • Member
      52 point Member
    • dotnetnoob
    • Member since 11-17-2006, 4:08 PM
    • Posts 372

    Thanks...I don't really understand your code though...I'm not sure how it would be used to store persistent values that can be accessed from any page? 

  • Re: best way to store globally accessible values?

    07-04-2009, 2:33 PM
    • Participant
      989 point Participant
    • Scott927
    • Member since 09-20-2006, 4:53 AM
    • Phoenix, AZ
    • Posts 182

    Application variables and the <appSettings> section of web.config are both valid places to store globally accessible values. I'm not sure I understand where your confusion is coming from. You'd just set the application variables (or create them in <appSettings>) and then call them in your code when you need to.

    Scott M Schluer
    ---
    MCPD: ASP.NET Developer 3.5
  • Re: best way to store globally accessible values?

    07-04-2009, 2:44 PM

     Hi,

    if your values a static across the application you can set in web.config file. in <appsettings>

    ex:

    <appSettings>

    <add key="Email" value="classified@lee.com"/>

    </appSettings>

    or the values will chages in runtime you can use sessions or public class with get and set methods

    Thanks :)

     

    Remember to click “Mark as Answer” on the post, if it helps you. Because It helps others to find the solution.

    Srinivas Kotra.


  • Re: best way to store globally accessible values?

    07-04-2009, 2:48 PM
    • Member
      52 point Member
    • dotnetnoob
    • Member since 11-17-2006, 4:08 PM
    • Posts 372

    Scott927:

    Application variables and the <appSettings> section of web.config are both valid places to store globally accessible values. I'm not sure I understand where your confusion is coming from. You'd just set the application variables (or create them in <appSettings>) and then call them in your code when you need to.

     

    Thanks Scott: My confusion stems from reading many contradictory statements on the 'net about whether or not it's "good" to use Application variables vs. other methods, such as cache or appsettings or classes...I have a tendency to find one way to do something without considering whether it's a good or bad coding practice (and also trying to apply legacy vbscript coding to .net), so I'm trying to figure out what the best practice is.

  • Re: best way to store globally accessible values?

    07-04-2009, 9:10 PM
    Answer
    • Star
      12,617 point Star
    • malcolms
    • Member since 06-12-2008, 4:38 AM
    • Melbourne, Australia
    • Posts 2,077

    Scott927 is right.  Store them in your appSettings seeing as though they're not user specific.  Then use the ConfigurationManager to retrieve them:

    string something = ConfigurationManager.AppSettings["YourSetting"];

    You'll need to add a reference to System.Configuration for that to work.

    Sincerely,
    Malcolm Sheridan

    Microsoft Certified Solution Developer
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as
    Answer" if a marked post does not actually answer your question.
  • Re: best way to store globally accessible values?

    07-04-2009, 9:46 PM
    • Participant
      989 point Participant
    • Scott927
    • Member since 09-20-2006, 4:53 AM
    • Phoenix, AZ
    • Posts 182

    Personally, I use the appSettings section of the web.config file a lot more than I use application variables. If you're just starting out with .NET development, I'd say use appSettings for relatively static values (i.e. an SMTP server for sending email, or a path to a particular directory for uploading files, etc...things that you probably won't change). If you anticipate the values changing a lot (or even a little) during runtime, it would be easier to set initial values in an application variable in global.asax and modify it whenever you need to.

    Having said that, you CAN programatically change the appSettings section of web.config, and could even write a wrapper class to abstract some of the relative complexities of doing it (it's not that hard and probably only a few lines of code), but typing: Application["VariableName"] = "Something else" is just easier when you're learning. To get started, I'd probably just do as I outlined above and as you get a little more familiar with .NET, you can branch out into a more in depth solution.

    In case you're curious as to what it would look like to update an appSetting value programmatically, take a look at this:

    public void Update(string key, string value) 
    {
    Configuration configuration =   WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); 
    AppSettingsSection appSettingsSection = 
      (AppSettingsSection)configuration.GetSection("appSettings"); 
       if (appSettingsSection != null) 
       {
           appSettingsSection.Settings[key].Value = value; 
           config.Save();
       }
    }

    Scott M Schluer
    ---
    MCPD: ASP.NET Developer 3.5
  • Re: best way to store globally accessible values?

    07-05-2009, 1:07 AM
    Answer
    • Participant
      1,726 point Participant
    • hs_jha
    • Member since 01-02-2007, 7:36 PM
    • Bangalore , India
    • Posts 468

    Using appSettings section of the web.config file to store persistant data is a good option.

    As web.config is an xml file it does not put much pressure on server resources unlike database or application object.

    But as far as maintainability is concerned saving in database has an edge as it is not always true that a web administrator is a programmer as well.

  • Re: best way to store globally accessible values?

    07-05-2009, 4:28 PM
    • Member
      52 point Member
    • dotnetnoob
    • Member since 11-17-2006, 4:08 PM
    • Posts 372

    Thanks everyone for the help...I have a much better understanding of the options now and appreciate everyone's input. 

Page 1 of 1 (13 items)