Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET
Article

Customizing SectionGroups and Sections in Web.config

Rate me:
Please Sign up or sign in to vote.
2.77/5 (21 votes)
1 Jun 2004 126.6K   643   26   13
To add our own SectionGroups and Sections in the Configuration sections of web.config.

Introduction

This article deals with how to customize web.config file in our application.

Already, we have facility to access Appsettings in web.config. In certain situations, we need to declare our own SectionGroups and Sections in web.config depending upon our application's needs. This will help the developer to access these values from the sections across the application.

Here, I have created two Sections under one SectionGroup.

Hope this article will help whenever you want to handle SectionGroups in your web.config.

Code

XML
'write the following in the web.config
<configuration>
<configSections>
<!--Config section Group and Sections Declaration-->
<sectionGroup name="Company">
   <section name="AssociatedCompany" 
     type="System.Configuration.NameValueSectionHandler,System"/>
   <section name="Subsidiary" 
     type="System.Configuration.NameValueSectionHandler,System"/>
</sectionGroup>
</configSections>
.
.
.
.
.
.
<!--This For Section Declaration-->
<Company>
<AssociatedCompany>
<add key="A1" value="AWCompany"/>
<add key="A2" value="AXCompany"/>
<add key="A3" value="AYCompany"/>
<add key="A4" value="AZWCompany"/>
</AssociatedCompany>
<Subsidiary>
<add key="S1" value="AWCompany"/>
<add key="S2" value="AXCompany"/>
<add key="S3" value="AYCompany"/>
<add key="S4" value="AZWCompany"/>
</Subsidiary>
</Company>
</configuration>

Now, write the following into the code behind (VB):

VB
Imports System.Collections.Specialized
'This Code will populate the Company Details into dropdownlist

 Dim config As New NameValueCollection
       
        'Getting Associated Company code from web.config
        config = ConfigurationSettings.GetConfig("Company/AssociatedCompany")
        If Not IsNothing(config) Then
            Dim inKeyCnt As Integer
            'Loop to populate all the company code in the dropdown list
            For inKeyCnt = 0 To config.Keys.Count - 1
               'DropDown List ID is ddlAssociation
      
                 ddlAssociation.Items.Add(config(inKeyCnt).ToString)
            Next

        End If
'Similary for Subsidiary also.

Note: if you' get any error while calling GetCOnfig() function then provide the public key and version in the Section declaration of web.config. Check the public key and version information from your machine.config file in your framework. For example:

XML
<section name="AssociatedCompany" 
  type="System.Configuration.NameValueSectionHandler, 
         System,Version=1.0.5000.0, Culture=neutral, 
         PublicKeyToken=b77a5c561934e089"/>
<section name="Subsidiary" 
  type="System.Configuration.NameValueSectionHandler,
         System,Version=1.0.5000.0, Culture=neutral, 
         PublicKeyToken=b77a5c561934e089"/>

Drop me, in case if you have, any suggestions.

Thank you once again.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 84314782-Dec-14 6:23
Member 84314782-Dec-14 6:23 
GeneralLogin.aspx Pin
Ajay Kale New27-Sep-10 0:06
Ajay Kale New27-Sep-10 0:06 
Questionretriving the custome sections info in the code Pin
ambpk20-May-09 1:25
ambpk20-May-09 1:25 
AnswerRe: retriving the custome sections info in the code Pin
angelvarun30-Aug-11 23:01
angelvarun30-Aug-11 23:01 
AnswerRe: retriving the custome sections info in the code Pin
ajaynarewade23-Sep-12 4:30
ajaynarewade23-Sep-12 4:30 
QuestionHow to overwrite settings in web,config Pin
anuragsji31-Oct-07 18:54
anuragsji31-Oct-07 18:54 
QuestionHow to overwrite settings in web.config Pin
anuragsji31-Oct-07 18:50
anuragsji31-Oct-07 18:50 
GeneralRight on the money Pin
Rob Sitter6-Feb-07 2:33
Rob Sitter6-Feb-07 2:33 
GeneralConfiguration file - Need help Pin
buddymanish27-Feb-05 22:20
buddymanish27-Feb-05 22:20 
GeneralRe: Configuration file - Need help Pin
Anonymous1-Mar-05 19:02
Anonymous1-Mar-05 19:02 
GeneralRe: Configuration file - Need help Pin
buddymanish1-Mar-05 22:04
buddymanish1-Mar-05 22:04 
GeneralNeed Help ! Pin
Anonymous11-Jan-05 3:39
Anonymous11-Jan-05 3:39 
GeneralRe: Need Help ! Pin
Anonymous11-Jan-05 18:49
Anonymous11-Jan-05 18:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.