Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4345324
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:01:16+00:00 2026-05-21T12:01:16+00:00

I’m using asp.net mvc. How or where do I store single pieces of data?

  • 0

I’m using asp.net mvc. How or where do I store single pieces of data? For eg. SubscriptionFee, or IsSiteOffline.

I asked a question about user-settings here. Should I do something like this for sitesettings or is there another way apart from the database? I’d like my user to change these settings from the site itself.

I will be using EntityFramework code-first and would love if I could do something like: settings.SubscriptionFee.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:01:17+00:00Added an answer on May 21, 2026 at 12:01 pm

    Typically, you’ll put those settings into the <appSettings> section of your Web.config file.

    A standard ASP .NET MVC 3 application comes with a few settings already (inside of the <configuration> element):

      <appSettings>
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
        <add key="MyCustomSetting" value="abcd1234" />
      </appSettings>
    

    To reference them in your application, you use the ConfigurationManager class:

    using System.Configuration;

    string value = ConfigurationManager.AppSettings["MyCustomSetting"];

    As was said before, though, it may be better to create a configuration table in your data back-end (ie, SQL Server or whatever you use) and grab them from there.

    In one of my (non-MVC) applications, I created a static SysProperties class that would use the application’s cache to keep them cached for at least 5 minutes. This example doesn’t use the Entity Framework, but it could very easily be adapted:

    public static class SysProperties
    {
        public static string SMTPServer
        {
            get
            {
                return GetProperty("SMTPServer").ToString();
            }
        }
    
        public static decimal TicketFee
        {
            get
            {
                return Convert.ToDecimal(GetProperty("TicketFee"));
            }
        }
    
        private static object GetProperty(string PropertyName)
        {
            object PropertyValue = null;
            if (HttpContext.Current != null)
                PropertyValue = HttpContext.Current.Cache[PropertyName];
    
            if (PropertyValue == null)
            {
                SqlCommand cmSQL = new SqlCommand("SELECT Value FROM tblProperty WHERE Name = @PropertyName");
                cmSQL.Parameters.AddWithValue("@PropertyName", PropertyName);
    
                DataTable dt = Functions.RunSqlCommand(cmSQL);
    
                PropertyValue = dt.Rows[0][0];
    
                if (HttpContext.Current != null)
                    HttpContext.Current.Cache.Insert(PropertyName, PropertyValue, null, DateTime.UtcNow.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);
    
                return PropertyValue;
            }
            else
            {
                return PropertyValue;
            }
        }
    }
    

    Note: You could use this same technique with the ConfigurationManager to retrieve these values from the Web.config file instead of from a database.

    Just another free-be, this is some code that I’ve used to take advantage of SQL Server’s SqlCacheDependency. You’d have to enable the SQL Server Broker, but this allows you to keep values cached in memory until the value has changed in SQL Server. That way, you don’t have an arbitrary 5-minute expiration.

    This function was intended retrieve things with a two-part identifier (like the full name of a user), so it takes three parameters:
    – The SQL query to run in case the value isn’t cached
    – The unique ID of the item you’re wanting to retrieve (ie, the User ID)
    – An arbitrary string that identifies the type of data (ie, “UserFullName”, “UserEmail”)

    You could very easily adapt this to retrieve things with one-part identifiers:

    // Static constructor ensures that SqlDependency.Start is called before
    // we try to use any SqlCacheDependencies   
    static Functions()
    {
        SqlDependency.Start(ConnectionString);
    }
    
    public static string GetUserFullName(string UserName)
    {
        return GetSqlCachedValue("SELECT FirstName + ' ' + LastName FROM dbo.tblUser WHERE UserName = @Id", UserName, "UserFullName").ToString();
    }
    
    public static string GetEventNameFromId(int Id)
    {
        return GetSqlCachedValue("SELECT EventName FROM dbo.tblEvents WHERE EventID = @Id", Id, "EventName").ToString();
    }
    
    private static object GetSqlCachedValue(string Query, object Id, string CacheName)
    {
        // Get the cache
        System.Web.Caching.Cache currentCache = HttpContext.Current.Cache;
    
        object Value = null;
    
        // We use a standard naming convention for storing items in the application's cache
        string cacheKey = string.Format("{0}_{1}", CacheName, Id.ToString());
    
        // Attempt to retrieve the value
        if (currentCache != null && currentCache[cacheKey] != null)
            Value = currentCache[cacheKey];
    
        // If the value was not stored in cache, then query the database to get the value
        if (Value == null)
        {
            // Run the query provided to retrieve the value. We always expect the query to have the @Id parameter specified, that we can use
            // to plug-in the Id parameter given to this function.
            SqlCommand Command = new SqlCommand(Query);
            Command.Parameters.AddWithValue("@Id", Id);
    
            // Generate a cache dependency for this query
            System.Web.Caching.SqlCacheDependency dependency = new System.Web.Caching.SqlCacheDependency(Command);
    
            // Run the query
            DataTable dt = RunSqlCommand(Command);
    
            if (dt.Rows.Count == 1)
            {
                // Grab the value returned
                Value = dt.Rows[0][0];
    
                // Save the value in the cache, so next time we don't have to query SQL Server
                if (currentCache != null)
                {
                    currentCache.Insert(cacheKey, Value, dependency);
                }
            }
        }
    
        // return the final value
        return Value;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need to clean up various Word 'smart' characters in user input, including but
In order to apply a triggered animation to all ToolTip s in my app,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.