SIGN UP MEMBER LOGIN:    
ARTICLE

Add Key and Values to the Web.config File Dynamically in ASP.NET

Posted by Rajendra Taradale Articles | ASP.NET Programming November 30, 2011
This article will help you add Key and Values dynamically to a web.config file.
Reader Level:
Download Files:
 

This article will help you add the Key and Value to the web.config file dynamically.

This is simple and useful application for those who wants dynamic changes in the web.config file. The sample source code allows you to change the key and value pair. There is
no need to open web.config file. The application also allows you to update and delete the key/value pair.

Before you start any operation, you need to select one of the option buttons - Add, Update, or Delete.

Let's have to UI design Home Page.

Step 1:

The UI for adding a new key/value looks like following:

WebConfig1.gif

For updating the previous tag, Just select the Key values from Dropdown and update the new values with previous one

WebConfig2.gif

For Delete Operation, select the Key from dropdown which you want delete.

WebConfig3.gif

WebConfig4.gif

WebConfig5.gif

Step 2:

Please find the Code (.cs)

protected void Add_values(object sender, EventArgs e)
    {
        if (TextBox1.Text == string.Empty && TextBox2.Text==string.Empty)
        {
               //RFC Added
        }
        else
        {
            string Key_c = TextBox1.Text.ToString().Trim();
            string Value_c = TextBox2.Text.ToString().Trim();
            Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");
            webConfigApp.AppSettings.Settings.Add(Key_c, Value_c);
            webConfigApp.Save();
            DisplayConfigTags();
            Label1.Visible = true;
            Response.Redirect("Default.aspx");
        }
    }

protected void update_values(object sender, EventArgs e)
    {

        Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");
        string Key_c = KeyList1.SelectedItem.Value;
        string Value_c = TextBox2.Text.ToString();
        webConfigApp.AppSettings.Settings[Key_c].Value = Value_c;
        webConfigApp.Save();
        Label1.Visible = true;
        Response.Redirect("Default.aspx");

        TextBox2.Text = string.Empty;
    }

protected void detele_values(object sender, EventArgs e)
    {
        string Key_c = KeyList1.SelectedItem.Value;
        string Value_c = TextBox2.Text.ToString();
        Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");
        webConfigApp.AppSettings.Settings.Remove(Key_c);
        webConfigApp.Save();
        Label1.Visible = true;
        Response.Redirect("Default.aspx");
    }

 

Download the attached source code for more details.

 

Login to add your contents and source code to this article
share this article :
post comment
 

Hi Dinesh...It's my pleasure to be part Thank's

Posted by Rajendra Taradale Dec 01, 2011

Thank you..:)

Posted by Rajendra Taradale Dec 01, 2011

Hi Rajendra. Nice article.

Posted by Monika Arora Dec 01, 2011

Good start, Welcome to the Mindcracker community.

Posted by Dinesh Beniwal Dec 01, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor