Click here to Skip to main content
Click here to Skip to main content

Customizing SectionGroups and Sections in Web.config

By , 1 Jun 2004
 

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>
<!--<span class="code-comment">Config section Group and Sections Declaration--></span>
<sectionGroup name="Company">
   <section name="AssociatedCompany" 
     type="System.Configuration.NameValueSectionHandler,System"/>
   <section name="Subsidiary" 
     type="System.Configuration.NameValueSectionHandler,System"/>
</sectionGroup>
</configSections>
.
.
.
.
.
.
<!--<span class="code-comment">This For Section Declaration--></span>
<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

No Biography provided

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralLogin.aspx PinmemberAjay Kale New27-Sep-10 0:06 
Questionretriving the custome sections info in the code Pinmemberambpk20-May-09 1:25 
AnswerRe: retriving the custome sections info in the code Pinmemberangelvarun30-Aug-11 23:01 
AnswerRe: retriving the custome sections info in the code Pinmemberajaynarewade23-Sep-12 4:30 
QuestionHow to overwrite settings in web,config Pinmemberanuragsji31-Oct-07 18:54 
QuestionHow to overwrite settings in web.config Pinmemberanuragsji31-Oct-07 18:50 
GeneralRight on the money PinmemberRob Sitter6-Feb-07 2:33 
GeneralConfiguration file - Need help Pinmemberbuddymanish27-Feb-05 22:20 
GeneralRe: Configuration file - Need help PinsussAnonymous1-Mar-05 19:02 
GeneralRe: Configuration file - Need help Pinmemberbuddymanish1-Mar-05 22:04 
GeneralNeed Help ! PinsussAnonymous11-Jan-05 3:39 
GeneralRe: Need Help ! PinsussAnonymous11-Jan-05 18:49 

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.6.130617.1 | Last Updated 2 Jun 2004
Article Copyright 2004 by Sadish Kumar.V
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid