Solved

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

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now