Update appSettings from code behind

て烟熏妆下的殇ゞ 提交于 2019-12-25 06:54:09

问题


I' trying to update key value from code behind and it seemed like web.config updated but value didn't saved. appSetting there is nothing special:

 <appSettings>
    <add key="Default" value="1.11"/>
    <add key="Company" value="1.078"/>
    <add key="Customer" value="1.1"/>
  </appSettings>

Here is the code that must update appSettings key value:

 Protected Sub btnSaveDefault_Click(sender As Object, e As System.EventArgs) Handles btnSaveDefault.Click
        Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration("/")
        config.AppSettings.Settings.Item("Default").Value = tbDefault.Text
        config.Save(ConfigurationSaveMode.Modified)
        ConfigurationManager.RefreshSection("appSettings")
    End Sub

回答1:


hope this help ...

System.Configuration.Configuration objConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");   

    objConfig.AppSettings.Settings["DEFAULT_PASSWORD"].Value = password;
    objConfig.Save();

follow this link .. Getting error while changing Appsettings value in Web config from code behind




回答2:


Well. I found solution myself. The problem was in the other piece of code. So each time on Page Load code giving me old value and If Page.IsPostBack fixed this issue.

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            tbCompany.Text = Convert.ToSingle(ConfigurationManager.AppSettings("Company"))
            tbDefault.Text = Convert.ToSingle(ConfigurationManager.AppSettings("Default"))
            tbUser.Text = Convert.ToSingle(ConfigurationManager.AppSettings("Customer"))
        End If
    End Sub


来源:https://stackoverflow.com/questions/20769771/update-appsettings-from-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!