Click here to Skip to main content
Licence 
First Posted 1 Jun 2004
Views 74,219
Bookmarked 22 times

Customizing SectionGroups and Sections in Web.config

By | 1 Jun 2004 | Article
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

About the Author

Sadish Kumar.V



India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralLogin.aspx PinmemberAjay Kale New0:06 27 Sep '10  
Questionretriving the custome sections info in the code Pinmemberambpk1:25 20 May '09  
AnswerRe: retriving the custome sections info in the code Pinmemberangelvarun23:01 30 Aug '11  
QuestionHow to overwrite settings in web,config Pinmemberanuragsji18:54 31 Oct '07  
QuestionHow to overwrite settings in web.config Pinmemberanuragsji18:50 31 Oct '07  
GeneralRight on the money PinmemberRob Sitter2:33 6 Feb '07  
GeneralConfiguration file - Need help Pinmemberbuddymanish22:20 27 Feb '05  
GeneralRe: Configuration file - Need help PinsussAnonymous19:02 1 Mar '05  
GeneralRe: Configuration file - Need help Pinmemberbuddymanish22:04 1 Mar '05  
GeneralNeed Help ! PinsussAnonymous3:39 11 Jan '05  
GeneralRe: Need Help ! PinsussAnonymous18:49 11 Jan '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 2 Jun 2004
Article Copyright 2004 by Sadish Kumar.V
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid