Question

I decided to use Properties.Settings to store some application settings for my ASP.net project. However, when trying to modify the data, I get an error The property 'Properties.Settings.Test' has no setter, since this is generated I have no idea what I should do to change this as all my previous C# Projects have not had this issues.

Was it helpful?

Solution

My guess is that you defined the property with the Application scope, rather than the User scope. Application-level properties are read-only, and can only be edited in the web.config file.

I would not use the Settings class in an ASP.NET project at all. When you write to the web.config file, ASP.NET/IIS recycles the AppDomain. If you write settings regularly, you should use some other settings store (e.g. your own XML file).

OTHER TIPS

As Eli Arbel already said you can’t modify values written in web.config from your application code. You can only do this manually but then the application will restart and this is something you don’t want.

Here is a simple class you can use to store values and make them easy to read and modify. Just update the code to suite your needs if you’re reading from XML or database and depending on whether you want to permanently store modified values.

public class Config
{
    public int SomeSetting
    {
        get
        {
            if (HttpContext.Current.Application["SomeSetting"] == null)
            {
                //this is where you set the default value 
                HttpContext.Current.Application["SomeSetting"] = 4; 
            }

            return Convert.ToInt32(HttpContext.Current.Application["SomeSetting"]);
        }
        set
        {
            //If needed add code that stores this value permanently in XML file or database or some other place
            HttpContext.Current.Application["SomeSetting"] = value;
        }
    }

    public DateTime SomeOtherSetting
    {
        get
        {
            if (HttpContext.Current.Application["SomeOtherSetting"] == null)
            {
                //this is where you set the default value 
                HttpContext.Current.Application["SomeOtherSetting"] = DateTime.Now;
            }

            return Convert.ToDateTime(HttpContext.Current.Application["SomeOtherSetting"]);
        }
        set
        {
            //If needed add code that stores this value permanently in XML file or database or some other place
            HttpContext.Current.Application["SomeOtherSetting"] = value;
        }
    }
}

Here: http://msdn.microsoft.com/en-us/library/bb397755.aspx

is the solution for your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top