I am trying to read the connection strings from my app.config but it only shows me one connection string and that too isn't in my app.config.
Here's my code:
System.Diagnostics.Debugger.Break();
Configuration config =
ConfigurationManager.OpenExeConfiguration(
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
foreach (var connectionString in config.ConnectionStrings.ConnectionStrings)
System.Diagnostics.Debug.Print(connectionString.ToString());
And it prints out this:
data source=.\SQLEXPRESS;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
When I am expecting it to print out a custom connectionString to one of my databases that I have specified in the app.config file.
Update
Thank you all for spending time on my problem and trying to help out. Although all of you are right in that I could simply use ConfigurationManager.ConnectionStrings
or access any other subsection this way, I believe when I do that, the configuration is read-only. I cannot make any changes to it, such as add a new connection string or remove an existing one. And I need to do that. Please let me know if there's a way to modify the config file at run-time.
It reads from your machine.config.
After <connectionStrings>
in app.config insert a <clear/>.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With