Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properties.Settings has no setter

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.

like image 399
clifford.duke Avatar asked Oct 03 '13 05:10

clifford.duke


2 Answers

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).

like image 193
Eli Arbel Avatar answered Oct 06 '22 10:10

Eli Arbel


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;
        }
    }
}
like image 20
Jon Mallow Avatar answered Oct 06 '22 12:10

Jon Mallow