locked
Store and Retrieve Setting from DB RRS feed

  • Question

  • User-1915458 posted

    Using C# and sql, I want to save a bunch of site setting/preferences in a db table.

    What/where is the best way to retrieve site settings and make them available throughout the site?

    I know I could do a sql query every time a value is needed, ie. name, slogan, admin email address, etc. but that doesn't seem efficient.

    Originally I thought storing all the values in the web.config appsettings and calling it from there, but a db would allow easier management. 

    So, is there a way to call all the settings from the db when the site starts and then make them available in the code-behind?

    For example, right now I define "sitename" in appsettings and call it with:

    Page.Title = WebConfigurationManager.AppSettings["SiteName"];

    Thanks for any help on this.

    Wednesday, January 27, 2016 3:39 AM

All replies

  • User-1716253493 posted

    Maybe you can use app_start or session_start event in global.asax

    Get the values from db, store it in application varibales or session variables

    application_start

    Application["sitename"] = "..." //get site name from db

    To get the values from application variable

    string sitename = Application["sitename"].ToString();

    or session variables

    string sitename = Session["sitename"].ToString();

    Wednesday, January 27, 2016 6:36 AM
  • User-693045842 posted

    Hi WebSAE,

    In any Web application some data will be frequently used and some data will be infrequently used. We can improve the performance of our ASP.NET application by loading in advance the frequently-used data,you could try to cache data at Application  Startup :

    http://www.asp.net/web-forms/overview/data-access/caching-data/caching-data-at-application-startup-cs

    With Regards,

    Wednesday, January 27, 2016 6:39 AM
  • User-821857111 posted

    or session variables

    string sitename = Session["sitename"].ToString();

    I wouldn't recommend that. You will have one copy of the site name in memory one the server for every current user. Session variables should only be used to store user-specific data such as their preferences e.g. preferred language. Application variables are the way to go for application-wide data.

    Wednesday, January 27, 2016 7:30 AM
  • User753101303 posted

    Hi,

    Ypu coud create a class that would expose the configuation using something such as Page.Title = App.Settings.SiteName;

    Behind the scene is could be stored in a web.config or read (and cached) from a db but basically you should then be able to easily change your mind without your app main code having to deal or assume anything about that.

    Sunday, February 14, 2016 1:02 PM