Write Modify Web.Config Programmatically At Run Time

Write Modify Web.Config ConnectionString And AppSettings Programmatically At Run Time In Asp.Net

In this post I'm going to explain how to write or modify CnnectionStrings and AppSettings programmatically at run time in Web.Config file in Asp.Net.

Write web.config programmatically
To write or modify web.config programmatically we need to use ebConfigurationManager Class in System.Web.Configuration namespace.



We can add write or modify any section of web.config programmatically, here i m describing how to modify and write connectionStrings and AppSettings section of Web.Config file.

For this first of all add this namespace in code behind of page.

using System.Web.Configuration;

I have put two textbox on the page to display on Page Load and write new or modify existing connectionString in web.config file on Button Click Event.

HTML SOURCE OF PAGE
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtKey" runat="server">
</asp:TextBox>
<asp:TextBox ID="txtConnectionString" runat="server">
</asp:TextBox>
<asp:Button ID="btnWrite" runat="server" 
            onclick="btnWrite_Click" 
            Text="Modify/Write" />
</div>
</form>

Write below mentioned code in code behind of page to show existing connectionString in web.config on page Load.

C# CODE
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          txtConnectionString.Text = ConfigurationManager.ConnectionStrings[0].ConnectionString;
            
        }
    }

VB.NET CODE
Protected Sub Page_Load(sender As Object, e As EventArgs)
 If Not IsPostBack Then

  txtConnectionString.Text = ConfigurationManager.ConnectionStrings(0).ConnectionString
 End If
End Sub

To modify name of existing connection string in web.config programmatically write below mentioned code in Click Event of Button.

C# CODE
protected void btnWrite_Click(object sender, EventArgs e)
    {
        Configuration connectionConfiguration = WebConfigurationManager.OpenWebConfiguration("~");
        connectionConfiguration.ConnectionStrings.ConnectionStrings["test"].Name = txtKey.Text;
        connectionConfiguration.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("connectionStrings");
    }

To modify value of existing connection string in web.config programmatically write below mentioned code in Click Event of Button.

C# CODE
protected void btnWrite_Click(object sender, EventArgs e)
    {
        Configuration connectionConfiguration = WebConfigurationManager.OpenWebConfiguration("~");
        connectionConfiguration.ConnectionStrings.ConnectionStrings["test"].ConnectionString = txtConnectionString.Text;
connectionConfiguration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");

VB.NET CODE
Protected Sub btnWrite_Click(sender As Object, e As EventArgs)
 Dim connectionConfiguration As Configuration = WebConfigurationManager.OpenWebConfiguration("~")
 connectionConfiguration.ConnectionStrings.ConnectionStrings("test").Name = txtKey.Text
 connectionConfiguration.ConnectionStrings.ConnectionStrings("test").ConnectionString = txtConnectionString.Text
 connectionConfiguration.Save(ConfigurationSaveMode.Modified)
 ConfigurationManager.RefreshSection("connectionStrings")
End Sub

To add or write new name and value of connection string in web.config at run time write below mentioned code

C# CODE
protected void btnWrite_Click(object sender, EventArgs e)
    {
        Configuration connectionConfiguration = WebConfigurationManager.OpenWebConfiguration("~");
        
        ConnectionStringSettings csAddNewConString = new ConnectionStringSettings(txtKey.Text,txtConnectionString.Text ,"System.Data.SqlClient");
        
 ConnectionStringsSection csNewConSection = connectionConfiguration.ConnectionStrings;
        
 csNewConSection.ConnectionStrings.Add(csAddNewConString);
        
        connectionConfiguration.Save(ConfigurationSaveMode.Modified);

        ConfigurationManager.RefreshSection("connectionStrings");
    }

VB.NET CODE
Protected Sub btnWrite_Click(sender As Object, e As EventArgs)
 Dim connectionConfiguration As Configuration = WebConfigurationManager.OpenWebConfiguration("~")

 Dim csAddNewConString As New ConnectionStringSettings(txtKey.Text, txtConnectionString.Text, "System.Data.SqlClient")

 Dim csNewConSection As ConnectionStringsSection = connectionConfiguration.ConnectionStrings

 csNewConSection.ConnectionStrings.Add(csAddNewConString)

 connectionConfiguration.Save(ConfigurationSaveMode.Modified)

 ConfigurationManager.RefreshSection("connectionStrings")
End Sub


To write or add new key and value of connectionstring in AppSettings Section of web.config we can write code as mentioned below.

C# CODE
protected void btnWrite_Click(object sender, EventArgs e)
    {
        Configuration connectionConfiguration = WebConfigurationManager.OpenWebConfiguration("~");
        
        connectionConfiguration.AppSettings.Settings.Add("writeAppSettingsString", txtConnectionString.Text);
        
        connectionConfiguration.Save(ConfigurationSaveMode.Modified);
        
        ConfigurationManager.RefreshSection("connectionStrings");
    }

VB.NET CODE
Protected Sub btnWrite_Click(sender As Object, e As EventArgs)
 Dim connectionConfiguration As Configuration = WebConfigurationManager.OpenWebConfiguration("~")

 connectionConfiguration.AppSettings.Settings.Add("writeAppSettingsString", txtConnectionString.Text)

 connectionConfiguration.Save(ConfigurationSaveMode.Modified)

 ConfigurationManager.RefreshSection("connectionStrings")
End Sub

To remove any existing connection string in AppSettings we can use Remove() method

connectionConfiguration.AppSettings.Settings.Remove("writeAppSettingsString");

Hope this helps.


If you like this post than join us or share

Find More Articles