Understanding ConfigurationManager Class in ASP.Net 2.0

Some of the values and strings are used across all the pages of a website. It is a tedious process to code those values and strings in each page of the website.

For example consider a connection string. The connection string will be used in many pages of the web site if you are developing a database driven website. Let us say that you have hard coded the connection string in all the pages, around 30 pages. Now if the value of the connection string changes, you have a headache of changing all the values of the connection string in all the 30 pages. This can’t be done easily and if you are to change the string again after about one month, it is going to be a tedious task.

Hence in such scenarios it is a practice to store those values in a central repository from where you can retrieve it and use it in all the pages. For example if you store the connection string in a central place like a web.config file you can retrieve those connection strings from the web.config file and use it. Previous to ASP.Net 2.0 the connection strings were stored in web.config file as given below:

<add key="cString" value="connection_string_value_here" />


The connection string is stored as key and value pair in the <appSettings> element of the web.config file. This value can be retrieved by writing code like:


Instead of using the <appSettings> section you can also add your own sections in the web.config file to have all your string value that you will be using in your application.

ASP.Net 2.0 provides with a special class for retrieving those customized string values. This class is called the ConfigurationManager class. Using this class you will be retrieving values by writing the following code for the above config file.


To open the ConnectionStrings section of the web.config file, you can use code like


The above code is used to retrieve ConnectionStrings from the web.config file as given below:

<add name="prodCS"
connectionString="Data Source="(local)";
Initial Catalog=Products;Integrated Security=True"
providerName="System.Data.SqlClient" />

This code comes under the <configuration> section of the web.config file.

The other methods that are available in the ConfigurationManager class are:

· GetSection
· GetWebAppSection
· RefreshSection
· OpenMachineConfiguration
· OpenMappedMachineConfiguration
· OpenExeConfiguration
· OpenMappedExeConfiguration
· OpenWebConfiguration
· OpenMappedWebConfiguration

The GetSection method of the ConfigurationManager class is used to retrieve a particular section from the configuration file in the current directory of the web application. If you need to get a particular section from the current web applications root directory you need to use the GetWebAppSection method of the ConfigurationManager class. The codes given below use these methods to retrieve the corresponding section from the corresponding config files.



The OpenMachineConfiguration method is used to open the machine.config file and the OpenMappedMachineConfiguration is used to open any specified machine.config file. Similarly the OpenExeConfiguration method is used to open a client’s configuration file and the OpenWebConfiguration is used to open the current web applications configuration files.

Thus we find that the ConfigurationManager class is used to retrieve, edit and update the configuration file’s values programmatically. This class is a new way of doing these tasks in ASP.Net 2.0.

| call external | localization | access listbox values | server components | html email send | microsoft exchange | code performance analysis using vs2005 | gac | j2ee | access control | creating self updating applications | tracing | webforms | visual inheritance | caching in aspnet | adrotator component | frames | creating multithreaded application | netwindows | xml tools | microsoft internet | file system watcher class | code access | web testing using vs2005 team system | cls | microsoft dataaccess | adrotator | jscript .net | mfc | net data access | cookie set retrieve | session state | guest book creation | repeater controls | microsoft sharepoint | web parts | activex | cryptography classes | publish feature in vs2005 | garbage collection | data binding |

“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.