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

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

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.
Who is Participating?
Salim FayadConnect With a Mentor Commented:
You put them in your web.config:

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

And in your code:

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

Open in new window

Daniel WilsonCommented:

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

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.