Centralised Connection String for multiple application asp.net - c#

I have a web server with multiple application running. All the application have their web.config file. If the database password changes due to Policy I have to manually change the password in each of web.config files in the app setting section.
I was reading about the connection string setting in machine.config file.
Now my question is if I put connection string in appsetting section of machine.config with name ConnectionString and same in my web.config file will it overwrite the machine.config file values.
In my machine.config following is the setting
<configuration>
....
<appSettings>
<add key="ConnectionString" value="value"/>
</appSettings>
</configuration>
similarly in my web.config file
<configuration>
....
<appSettings>
<add key="ConnectionString" value="value"/>
</appSettings>
</configuration>
And I get the value in my code as below
string conString=ConfigurationManager.AppSettings["ConnectionString"];
will I get the overloaded value?

What's going to help you out here is to store your connection string(s) in .config file and then reference them either using the file="" attribute or the configSource="" attribute.
Here's an excellent question and answer that talks about the differences between the two and shows you how to implement them:
ASP.NET web.config: configSource vs. file attributes

Related

Making machine.config with multiple external config files

I've been trying to make the machine.config to be clean by adding the connectionString section and appSettings section externally.
It works well with just making the connectionString externally only like below.
Machine.config
<configuration>
<connectionStrings configSource="connectionStrings.config"/>
....
<appSettings>
....
</appSettings>
</configuration>
connectionStrings.config is the same folder where machine.config like below
<connectionStrings>
<add name= .../>
...
</connectionStrings>
But when trying to add the appSettings section as an external config additionally, it seems the application does not read the appSettings section properly. (By debugging under VS, the value of a key in the section does not return anyting and it is empty)
Of course, I tried to both ways when making appSettings external as below:
<appSettings configSource="AppSettings.config"/>
or
<appSettings file="AppSettings.config"/>
so my question is what I am trying to is not possible?
I refer the below article.
http://blog.andreloker.de/post/2008/06/16/Keep-your-config-clean-with-external-config-files.aspx
Here is my working environments:
Windows 7 x64
machine.config that I am working is c:\windows\Microsoft.net\Framework64\v2.0.50727\config\machine.config
The application is x86 build.

Getting ConnectionString of a BizTalk config file from a C# Application

I have a BizTalk config file that I want to use in my C# application. I'd like to get the connection string from the BizTalk config. Is there a way to do this? Simply put, I want to read a connection string from an external config file.
What I'm currently using in my C# app config is:
<configuration>
<appSettings>
<add key="foo" value="blah;"/>
<add key="foo" value="blah;"/>
</appSettings>
</configuration>
I get the keys by using this code:
connectionString = ConfigurationManager.AppSettings[configKey];
Thanks.
From an external .config file, as in not the current .exe's config file, no.
System.Configuration will always refer to the local .config.
To access another .exe's .config file, you have to treat it as just an Xml file with System.Xml.

ConfigurationManager does not point to correct app.config

I am trying to use ConfigurationManager to get the connection string from a project call FileShareAccessLibrary.
This is the code I am writting in order to do this:
ConfigurationManager.ConnectionStrings["FileShareAccessLibrary"].ConnectionString
This is the content of app.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections></configSections>
<connectionStrings>
<add name="FileShareAccessLibrary" connectionString="......"
providerName="System.Data.SqlClient" />
<add name="FileShareAccessLibrary.Properties.Settings"
connectionString="..."
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
When I run my app I get a NullReferenceException because ConfigurationManager.ConnectionStrings["FileShareAccessLibrary"] returns null.
While debugging I noticed that none of the two connection strings are stored inside ConfigurationManager.ConnectionStrings so I figured that the ConfigurationManager is pointing to another file.
In my project I have not other app.config file.
Is there something I am doing wrong here?
Why is ConfigurationManager not getting my connection string?
If your FileShareAccessLibrary project is a class library rather than a windows forms application or console application then you will need to move the connection strings (and app settings if you have any) from the FileShareAccessLibrary config file to the config file of the application(s) that reference the FileShareAccessLibrary dll.

Is it possible to read a web.configuration file and add some tags in another web.config file?

Let us consider a web.config file deployed from website....
let us take another project say console application or some class file then is it possible to read that web.configuration file and add some tags in that web.config file
i just want to add these lines in to it
<system.webServer>
<modules>
<add name="sample" type="sample.class" />
</modules>
</system.webServer>
Waiting for your valuable comments
Updated: Read this Article and download source code, it may help you to read/write new section in config files.
To read the AppSettings from the Web.Config file you can use the ConfigurationSettings or ConfigurationManager Class.
It works like the following
string connectionInfo = ConfigurationSettings.AppSettings["ConnectionInfo"];
ConnectionInfo is in the Web.config file under the AppSettings like
<appSettings>
<add key="ConnectionInfo" value="server=(local);database=Northwind;Integrated Security=SSPI" />
</appSettings>
and to write in the web.config AppSettings section.. Let's say you have a key 'SiteName' in your appsettings section of your config file... you can update it like that..
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
config.AppSettings.Settings["SiteName"].Value = "New Site Name Value";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

connection string issue in web.config of ASP.Net

I am using VSTS 2010 + C# + .Net 4.0 to develop an ASP.Net application using SQL Server 2008 Enterprise as database. I am learning someone else's code. I notice code like this, I am not sure whether the usage is correct and so I come here to ask for advice.
In the code, I see some code like this, I want to know whether using such method to read connection string from web.config is correct?
ConfigurationManager.ConnectionStrings["DBConnectinString"].ConnectionString
and such code is used to read connection string from web.config like below, please notice that connection string is defined outside of system.web section.
<?xml version="1.0"?>
<configuration>
<configSections>
<!--this section is empty-->
</configSections>
<appSettings>
...... content of appSettings
</appSettings>
<connectionStrings>
<add name="DBConnectinString" connectionString="data Source=.;uid=foo;pwd=foo;database=FOODB" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
... ... content of system.web
</system.web>
</configuration>
That is the standard place to define connection strings in web.config, yes.
See this for more info: http://msdn.microsoft.com/en-us/library/ms178411.aspx

Categories