Adding the settings in the Configuration file from code behind.


In this article we will discuss how can we access the configuration files section in the code behind pages and then how can we add the settings in the configuration sections. In this article we will add the settings in appsettings and connectionstring in web.config file from code behind.

In the design page we will take two buttons one each for appsetting and connection
string.And 4 buttons in which user will enter the key and value for adding in configuration
file. Now on the button clicks we will access the configuration file
using WebConfigurationManager and then add the settings.

Below is the desing page code for your reference:


<table cellpadding="5" cellspacing="5" class="style1">
<tr>
<td class="style3">
</td>
<td class="style5" style="text-align: center">
<asp:Label ID="Label1" runat="server"
style="font-size: medium; font-family: Verdana; font-weight: 700"
Text="Application Settings"></asp:Label>
</td>
<td class="style2" rowspan="2">
<asp:Button ID="btnaddappsetting" runat="server" Height="40px" onclick="btnaddappsetting_Click"
style="font-size: large; font-family: Verdana" Text="Save" Width="165px" />
</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="lbl_appsettingkey" runat="server"
style="font-size: medium; font-family: Verdana" Text="Key"></asp:Label>
</td>
<td class="style6">
<asp:TextBox ID="txt_appsettingkey" runat="server" Height="25px" Width="220px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="lbl_appsettingvalue" runat="server"
style="font-size: medium; font-family: Verdana" Text="Value"></asp:Label>
</td>
<td class="style6">
<asp:TextBox ID="txt_appsettingvalue" runat="server" Height="25px" Width="220px"></asp:TextBox>
</td>
<td>
 </td>
</tr>
<tr>
<td class="style4">
 </td>
<td class="style6" style="text-align: center">
<asp:Label ID="Label4" runat="server"
style="font-size: medium; font-family: Verdana; font-weight: 700"
Text="Connection Strings"></asp:Label>
</td>
<td>
 </td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="lbl_connectionsettingkey" runat="server"
style="font-size: medium; font-family: Verdana" Text="Key"></asp:Label>
</td>
<td class="style6">
<asp:TextBox ID="txt_connectionsettingkey" runat="server" Height="25px" Width="220px"></asp:TextBox>
</td>
<td rowspan="2" style="text-align: center">
<asp:Button ID="btnaddconnectionsetting" runat="server" Height="40px" onclick="btnaddconnectionsetting_Click"
style="font-size: large; font-family: Verdana" Text="Save" Width="165px" />
</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="lbl_connectionsettingvalue" runat="server"
style="font-size: medium; font-family: Verdana" Text="Value"></asp:Label>
</td>
<td class="style6">
<asp:TextBox ID="txt_connectionsettingvalue" runat="server" Height="25px" Width="220px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4" colspan="3">
<asp:Label ID="lblmessage" runat="server"
style="font-size: large; font-family: Verdana; font-weight: 700"></asp:Label>
</td>
</tr>
</table>

Note:

WebConfigurationManager will access the configuration file.
OpenWebConfiguration will open the configuration file.


protected void btnaddappsetting_Click(object sender, EventArgs e)
{
string key = txt_appsettingkey.Text;
string value = txt_appsettingvalue.Text;

Configuration c = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
c.AppSettings.Settings.Add(key, value);
c.Save();
lblmessage.Text = "App settings saved";

}
protected void btnaddconnectionsetting_Click(object sender, EventArgs e)
{
Configuration c = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConnectionStringSettings cs = new ConnectionStringSettings();
cs.Name = txt_connectionsettingkey.Text;
cs.ConnectionString = txt_connectionsettingvalue.Text;
c.ConnectionStrings.ConnectionStrings.Add(cs);
c.Save();
lblmessage.Text = "Connection strings saved";

}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: