Solved

how to loop through web.config and check all items in a section?

Posted on 2008-10-10
2
264 Views
Last Modified: 2013-12-17
i want to test DB connections as sort of a DB health check. i want to have a section in the web config with 6 connection strings called things like DBNewYork, DBCali, DBFlorida... and for a .aspx page to loop over all of them and connect do do some silly select 'true' or something to show the connection worked and DB send a response back.

the ourput of the page should be
[DB name in config1] = true
[DB name in config2] = false
[DB name in config3] = true
...6

this will be nice since to add another DB to check all an admin would do is add a config item and save the new file.
0
Comment
Question by:supergirl2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
Salim Fayad earned 500 total points
ID: 22689788
You put them in your web.config:

<configuration>
     <connectionStrings>
              <add name="ConnectionString1" connectionString="...." />
              <add name="ConnectionString2" connectionString="...." />
              <add name="ConnectionString3" connectionString="...." />
     </connectionStrings>
</configuration>


And in your code:

foreach(ConnectionStringSettings objConnectionString in ConfigurationManager.ConnectionStrings)
{
       string connectionString = objConnectionString.ConnectionString;
}

Open in new window

0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22689793
http://msdn.microsoft.com/en-us/library/ms178411.aspx




Dim rootWebConfig As System.Configuration.Configuration
    rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot")
 
Dim connString As System.Configuration.ConnectionStringSettings
for each connString in rootWebConfig.ConnectionStrings.ConnectionStrings
  'do your thing using connString.ConnectionString as your connection string
next

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question