Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Customizing SectionGroups and Sections in Web.config

0.00/5 (No votes)
1 Jun 2004 1  
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

'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):

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:

<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