Reference connection strings in config from a separate project - c#

I have a parent application which has a config file that contains connection strings.
I would like all child applications I develop to be able to reference this config file so I only have to update connection strings in one place.
Parent Application web.config
<connectionStrings configSource="ConnectionStrings.config"/>
Parent Application ConnectionStrings.Config
<?xml version="1.0"?>
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=***;Initial Catalog=***;Integrated Security=False; Password=***;User ID=***" providerName="System.Data.SqlClient" />
</connectionStrings>
This works fine for bringing in data in the parent application.
I've added the ConnectionStrings.config file to my child application as a linked file and then reference it in the web.config as follows:
Child Application web.config
<connectionStrings configSource="ConnectionStrings.config"/>
But when I run the child application I get the following error:
Unable to open configSource file 'ConnectionStrings.config'.
(C:\Child Application\web.config line 8)
Any ideas?
Thanks

Under the child project, open the file properties of the linked ConnectionStrings.config file and set Copy To Output Directory to Copy always. You have to do this in order for this file to be copied to the child project bin directory regardless of the setting for the parent project ConnectionString.config file.

You could also use project post build steps and copy configuration file into output directory.

Related

Save Database Settings to be accessible by all projects in solution

I've got a solution with 4 projects (Windows Service, Windows forms, Web and Shared code).
I would like all 4 apps to be able to write and read the database connection settings (server, db name, credentials) to an area where all apps can read it.
Currently it is being saved in the config file, but the Shared code app does not save it it's own config file, but in the app.config and web.config files respectively.
Is there a way that I can save it in a general space to be accessible by all?
Create another configuration file in the parent of all these apps in which you'll store the connection string.
Then customize the loading of the config file to take this file into account.
The appSettings element can contain a file attribute that points to an external file. I will change my web.config file to look like the following:
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="false" strict="false" explicit="true" />
</system.web>
<appSettings file="../externalSettings.config"/>
</configuration>
Next, we can create the external file "externalSettings.config" and add an appSettings section with our connection information and any other settings that we want to use.
Reading connection string from external config file
You would also need to save the settings you want in that particular file

Where to put EF Connection String?

I am working on a project with plugin architecture in VB.NET. I have two projects: PublicInterfaces which includes my IPlugin interface and some other codes which will be shared on other projects, and DemoApp which has a reference to PublicInterfaces's assembly.
My Data model is and edmx file is located in PublicInterfaces, but in run-time, it gives me an error like this:
No connection string named 'HRMSApplicationEntities' could be found in the application config file.
while my App.config File of PublicInterfaces contains it!
<connectionStrings>
<add name="HRMSApplicationEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=HRMSApplication;user id=sa;password=Hamckerma;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
But the strange thing is when I put this into DemoApp's App.config file, IT WORKS WITH NO ERRORS!
Where should I put the connection string?
When you build DemoApp, you will have DemoApp.exe.config in debug/release folder, which is the application's configuration file.
DemoApp and every referenced dll (including PublicInterfaces.dll) will look in this configuration file. So you only need to add connection string in DemoApp's App.config file and it should work fine.

Object reference not set to an instance of an object in windows service

Read a app.config value:
string configFile =
System.Configuration.ConfigurationManager.AppSettings["connStr"].ToString();
Config file:
<appSettings>
<add key="connStr" value="Data Source=Dolphin-PC;
Initial Catalog=jsptpd_SYS;
Persist Security Info=True;User ID=sa;
Password=ccir"/>
</appSettings>
Get an error:
Object reference not set to an instance of an object
the \program and debug\ folder contains app.config file.
Why woud this happen
Because, System.Configuration.ConfigurationManager.AppSettings["connStr"] is null
How could i deal it?
A more valid question. There isn't anything wrong with your code here. the main reason that you are finding it null is probably because your app.config is not present in the directory you are running the application from. It could be because app.config is part of some library and it is not copied to the output directory. You need to make sure that either app.config is in your main project or you copy it to the directory (you are running your applicatior from) explicitly.
To fixed the problem,you need to know the princeple of ConfigurationManager how to read the config file:
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx
To solve this problem,you could make sure your bin/ directory generate yourprogram.exe.config file,so the ConfigurationManager could read.
If this file missing,you coud make sure your app.config attribute generate operation is not Embeded resource.If it is true,the app.config will embeded to the exe file and configurationmanager could not read.

Centralize connection strings for multiple projects within the same solution

I currently have three projects in my solution that all have their own App.config file with the same exact connection string.
Is there a way to consolidate the connections strings / app.config files so that I only need to make changes to one location?
You can share the connection strings among multiple projects in a solution as follows:
Create a ConnectionStrings.config file with your connection strings under a solution folder, this file should contain only the section connectionStrings
In your projects, add this config file As a Link (add existing item, add as link)
Select the added file and set its property Copy to Output Directory to Copy always or Copy if newer
In the App.config of your projects, point to the linked ConnectionStrings.config file using the configSource attribute:
<connectionStrings configSource="ConnectionStrings.config" />
ConnectionStrings.config
<connectionStrings>
<add name="myConnStr" connectionString="Data Source=(local); Initial Catalog=MyDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
...
<connectionStrings configSource="ConnectionStrings.config" />
...
</configuration>
Read more details....
There's a few ways you could do it:
Put common configuration settings in machine.config as shown here
Put common configuration settings in a central file and link to that in each projects's app.config as shown here
Store the configuration settings in the registry
For me, i always work with the last solution :)
Good luck!
First, take a look at this post. It describes how you can share the same app.config between multiple projects.
How to Share App.config?
Second, take a look at one other post, which describes how you let different app.config-files have a reference to one single shared xml-file which contains the connection strings.
Use XML includes or config references in app.config to include other config files' settings

How can I access the connection string in my Web.Config file through a Windows Application

I have my Entity Connection String set in my Web.Config file. In the same solution, I have a Windows Application, and I want to access the Connection String of my Entity. How can I access the connection string that exist in my Web.Config file? Or I have to create a specific one in my Windows Application ?
If you're unfamiliar with adding a config file to a windows app, you must select an "Application Configuration File" from add new items, it will create a file named App.Config which will at compile time become YourProgramsName.exe.config but internally it works mostly just like a web.config with an appsettings section and connectionstrings sections and all the other normal sections.
The best way to share this data between Web and Win config files would be:
Create a separate file that looks like:
<connectionStrings>
<add name="Name"
providerName="System.Data.ProviderName"
connectionString="Valid Connection String;" />
</connectionStrings>
and reference it from both config files with:
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings configSource="connections.config"/>
</configuration>
Then just add the connections.config (add existing item by link) to each project.
More reading: http://msdn.microsoft.com/en-us/library/ms254494.aspx
Put the connection string your windows application's App.config.
use this code:
string connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

Categories

Resources