Solved

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

Posted on 2008-10-10
2
259 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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