Home / ASP.NET Wiki / Configuration / How to edit Configuration file Web.Config

How to edit Configuration file Web.Config

 Rate It (2)

How to edit Configuration file Web.Config

in web.config we use <appSettings> section to add keys as you see

  <appSettings>

    <add key="email" value="" />

   <add key="color" value="green" />

  </appSettings>

in code behind write this code to enable us reach to Keys and get value

 Configuration conf = WebConfigurationManager.OpenWebConfiguration("~");

        AppSettingsSection app = (AppSettingsSection)conf.GetSection("appSettings");

        app.Settings["email"].Value = TextBox1.Text;

        app.Settings.Add("color", "green");

        conf.Save();

hope this useful 

Revision number 2, Wednesday, August 22, 2012 2:43:13 PM by tmorton

Comments

I can understand the first code easily. But sorry to say about the second one. That's kinda difficult! Can I get inboxed for some elaborations?

Ok, The second part of this code. it talks about how to access appSettings keys to allow us adding, editing, removing specific key. Configuration conf = WebConfigurationManager.OpenWebConfiguration("~"); this means accessing to web.config file that allows us to access all sections in this file. AppSettingsSection app = (AppSettingsSection)conf.GetSection("appSettings"); this means accessing to specific section "appSettings". app.Settings["email"].Value = TextBox1.Text; here we have to make editing on value of "email" key. app.Settings.Add("color", "green"); in this line we have to make adding new key. finally we must save all changes using conf.Save();. hope to understand that clearly

Related Articles

Employee Info Starter Kit - Getting Started

Employee Info Starter Kit is an open source ASP.NET project template that is intended to address different types of real world challenges faced by web application developers when performing common CRUD operations. Using a single database table ‘Employee’,

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. francissvk (1)
  2. deepeshsp (1)