Customizing SectionGroups and Sections in Web.config in C#


Hi Friends,
 some time  we need to declare our own SectionGroups and Sections in web.config depending upon certain situations.  here i am explaining you how to access and use the Section values in your 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.


<configuration>
<configSections>
 <sectionGroup name="SectionGroupName"> //Here we declare the name of the section group
   <section name="SectionName1" 
     type=="System.Configuration.NameValueSectionHandler,System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowLocation="true" allowDefinition="Everywhere"/>//Section name 1
   <section name="Sectionname2" 
     type=="System.Configuration.NameValueSectionHandler,System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowLocation="true" allowDefinition="Everywhere"/>//Section name 2
</sectionGroup>
</configSections>


------------------------Here is the Syntax of Section Group--------------------------------

<sectionGroup 
   name="section name"
   type="configuration section handler class, assembly file name, version, culture, public key token">
   <section />
/>

The following sections describe attributes
Attribute
Description
Name
Required String attribute.
Specifies the name of the configuration section or element that is associated with the configuration section handler that is specified by the typeattribute. This is the name of the group element as it appears in the section settings area


type
Required String attribute.
Specifies the name of the configuration section handler class that handles the processing of the configuration settings that are in the section or element that is specified in the name attribute. Use the following format:
type=" Fully qualified class name , assembly file name, version, culture, public key token"
The definition must match the assembly reference. For example, if the version number in the following code example does not match the assembly, an error occurs.
type="MyConfigSectionHandler.MyHandler,MyCustomConfigurationHandler,Version=1.0.0.0,Culture




---------------Assign the Value of Sections-----------------------
<configuration>

<SectionGroupName>
<SectionName1>
<add key="A1" value="AWCompany"/>
<add key="A2" value="AXCompany"/>
</SectionName1>
<SectionName2>
<add key="S1" value="AWCompany"/>
<add key="S2" value="AXCompany"/>
</SectionName2>
</SectionGroupName>
</configuration>


----------------Access the value on page------------------------


                System.Collections.Specialized.NameValueCollection collectionObj=
                  (System.Collections.Specialized.NameValueCollection)
                  System.Configuration.ConfigurationSettings.GetConfig("SectionGroupName/SectionName1"); 
                collectionObj.Get(0).ToString();//Here Get(0) is first index value of section
              




Hope this article will help you...


Regards 
Rajesh


0 comments: