……………………………………………….Expertise in .NET Technologies

  • Categories

Storing Connection String Information in Web.Config

Posted by Ravi Varma Thumati on November 2, 2009

You can store the connecting string information in your web.config file in two ways:

  •  Using <appSettings> Section
  •  Using <CustomSection> Section.

Using <appSettings> Section

The predefined <appSettings> section can be placed in any web.config file or the machine.config file. This section is useful for storing name-value pairs of data. An example of the use of the <appSettings> section is as follows,

       <add key=”ConnectionString” Value=”myconnectionstring” /> 

To access the appSettings Value, you need to use ConfiugrationSttings.AppSetting[“ConnectionString”]. This uses the NameValueFileSectionHandler, which returns a System.Collection.Specialized.NameValueCollection object. The collection implements the IEnumerable interface so you can enumerate the collection, or read values directly for any valid key.

Using <CustomSection> Section

You can create your own custom sections in a configuration file. The easiest way to do this is to configure one of the pre-existing configuration section handlers, assuming your section uses one of the generic structures such as name-value data, or single tag data. In this particular case you might use the Name-Value data. To do this, you need to use the pre-defined section handlers.

 For example, you can define a section called , <myNameValueSection>

         <add key=”ConnectionString” Value=”my connection string”

To read the data, you will need the following code snippet ,

NameValuecollection config = ConfigurationSettings.GetConfig(“myNameValueSection”)

For each key in config.keys
       label1.Text = “Key:” + key.toString()
       label2.Text = “Value:” + config[key]
End Loop



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: