Set the value for Custom Section NameValueSectionHandler in Webconfig

I have custom section Links which I Should edit from UI. While saving I couldn't save the value of the corresponding key. It shows "collection is read only" error. Please help me to resolve this. Thanks in Advance.

WebConfig:

<sectionGroup name="Menu">
  <section name="Links" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>

C#

NameValueCollection nameValueCol = (NameValueCollection)ConfigurationManager.GetSection("Menu/Links");
var Key = "Key";
var Value = "Value";               
nameValueCol.Set(Key, Value); // Error: collection is read only

so I tried to remove and add the keys still it shows same error.

nameValueCol.Remove(Key);    // Error: collection is read only
ameValueCol.Add(Key, Value);

Answers


Can you try it this way: In web.config add section and implement your own section handler

    <sectionGroup name="Menu">
       <section name="Links" type="Solution.Namepsace.KeyHandler"/>
   </sectionGroup>

Then, in your KeyHandler class you can access values for you config:

public class KeyHandler: ConfigurationSection
{
    #region Configuration Properties
    [ConfigurationProperty("KeyValueCollection")]
    public string KeyValueCollection
    {
        get { return this["KeyValueCollection"] as KeyValueCollection; }
    }
    #endregion
}

Class KeyValueCollection would look like this:

 [ConfigurationCollection(typeof(KeyValue), AddItemName = "KeyValue")]
public class KeyValueCollection : ConfigurationElementCollection
{ 
        get { return base.BaseGet(index) as KeyValue; }
        set
        {
            if (base.BaseGet(index) != null)
            {
                base.BaseRemoveAt(index);
            }

            this.BaseAdd(index, value);
        }
}

And lastly add KeyValue class:

public class KeyValue: ConfigurationElement
    {
        #region Constructor(s)
        public KeyValue()
        {
        }

        public KeyValue(string key, string value)
        {
            this.Key= key;
            this.Value= value;
        }
        #endregion

        #region Configuration Properties
        [ConfigurationProperty("Key", IsRequired = true)]
        public string Key
        {
            get { return this["Key"] as string; }
            set { this["Key"] = value; }
        }

        [ConfigurationProperty("Value", IsRequired = true)]
        public string Value
        {
            get { return this["Value"] as string; }
            set { this["Value"] = value; }
        }
        #endregion
    }

Need Your Help

Django Admin passing data to change_list_result.html

django django-models django-forms django-admin django-views

The template change_list_results.html recieves a parameter which is called results. It contains every record (in db) in a raw_html for printing it in change_list.html.

Record tablet's screen AND audio

android adb screen-recording

I've been wondering, after trying this command on my Nexus 7: