locked
update web.config custom values in asp.net RRS feed

  • Question

  • User-861774119 posted

    I want to update some values in the web,config through c# code. Here below I am giving the config file which I have,

    <configuration> 
     <applicationSettings>
        <ProjectName.Properties.Settings>
           <setting name="Employees" serializeAs="Xml">
            <value>
              <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                <string>000001;PSWD;DId\Emp1;Password</string>           
              </ArrayOfString>
            </value>
          </setting>
        </ProjectName.Properties.Settings>
      </applicationSettings>
    </configuration>

    Here in this, based on the employee(DId\Emp1) I want to update the password through c# code.

    Wednesday, December 6, 2017 10:06 AM

All replies

  • User475983607 posted

    You should store user accounts in a database table or file not the web.config as the application restarts each time the web.config is updated and saved.  

    Click the learn link above to start learning how to work with data.

    https://www.asp.net/web-forms/overview/presenting-and-managing-data

    Wednesday, December 6, 2017 12:29 PM
  • User-335504541 posted

    <g class="gr_ gr_13 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="13" data-gr-id="13">Hi</g> <g class="gr_ gr_14 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="14" data-gr-id="14">ocammu</g>,

    I think you could try to use WebConfigurationManager to find the value section of the applicationSettings.

    Then replace it with a new one.

    Please try to use the following code. It works.

                var configFile = WebConfigurationManager.OpenWebConfiguration("~");
                ClientSettingsSection section = (ClientSettingsSection)configFile.SectionGroups["applicationSettings"].Sections[0];
                var value = section.Settings;
                SettingElement settingElement = new SettingElement();
                SettingValueElement settingValueElement = new SettingValueElement();
                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath("~/Web.config"));
                XmlNodeList nodeList;
                XmlNode root = doc.DocumentElement;
                nodeList = root.SelectNodes("applicationSettings");
                var valuenode = nodeList[0].SelectNodes("ProjectName.Properties.Settings/setting/value")[0];
                var oldValue = valuenode.InnerText;
                valuenode.InnerText = "New Value1";
                settingValueElement.ValueXml = valuenode;
                settingElement.Value = settingValueElement;
                value.Clear();
                value.Add(settingElement);
                configFile.Save();

    Best Regards,

    Billy

    Thursday, December 7, 2017 6:33 AM