Reference another projects config file at debug - c#

I have two projects in a solution in Visual Studio 2010, Project1 and Project2.
Project1 is the parent project and has a config file called ConnectionStrings.config that contains connection strings used by both projects.
I have added the ConnectionStrings.config file from Project1 into Project2 as a linked file.
When I publish this to our webserver it works fine, Project2 can read data in from our database using the connection strings from the config file in Project1.
When I'm working on local and debug Project2 I get the following error which I assume means that it can't see the linked file:
Unable to open configSource file 'ConnectionStrings.config'.
(C:\Project2\web.config line 10)
Project1 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>
Project2 web.config
<?xml version="1.0"?>
<configuration>
<connectionStrings configSource="ConnectionStrings.config"/>
</configuration>
How can I get Project2 to read this config file from Project1?
Thanks

NO, I don't think you can do that. If you really want the same connection string to be shared across multiple project then consider having that connection string specified in machine.config rather.

Related

Live Unit Testing "Unable to open configSource file" error

I have a Helper project H, with a config file that is to be used as a shared configSource file from other projects.
The code in file (SharedConnectionString.config):
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
<add name="myConn" connectionString="Data Source=MyServer;Initial Catalog=MyDb;Persist Security Info=True;User ID=Username; password=PW;" providerName="System.Data.SqlClient" />
</connectionStrings>
I have a test project A that have it's own App.config and the SharedConnectionString.config added as file link
Config files in project A:
On the App.config I have:
<connectionStrings configSource="SharedConnectionStrings.config" />
Now, the test project compiles, the unit tests run successfully but the Live Unit Test feature from Visual Studio presents an error:
I tried the solution in here and here. None of there fixed the problem.
Opened an issue with Microsoft to see if help comes from somewhere..

App.Config Connection String

In my windows form i have connection string in app.config as
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="Database"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database.accdb"
providerName="System.Data.OleDb" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
And in all classes i am using the following code to connect to the database.
string connString = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;
But its not connected to the database.
Can somebody point out the mistake.
But when i use this code without use of app.config it works fine.
string connString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source = C:\\Users\\Amrit\\Desktop\\Database.accdb; Persist Security Info = False;";
How can i make the app.config connection string work..
You may do it so
<configuration>
<appSettings>
<add key="ApplicationTitle" value="Sample Console Application" />
<add key="ConnectionString"
value="Server=localhost;Database=Northwind;Integrated
Security=false;User Id=sa;Password=;" />
</appSettings>
then use ConfigurationSettings.AppSettings["ConnectionString"];
It seams (from the comments) that you are targeting two difference database files in those two connection strings. The first one is in your App_Data folder of your project, and the second one resides on your desktop.
The file in your App_Data folder is copied in to the output folder (bin/Debug or bin/Release for a WinForms project) every time you start the project in the VS. It overwrites previous contents of the file so every time you have a fresh copy of the file form the App_Data folder in your output folder. To find out, run the program and execute a few insertions. Then close the program and open the database file in the output folder (not in projects App_Data).
This happens because you have set the Copy to Output Directory property of the database file to Copy always.
you need to set DataDirectory.
You can then set the path in Application_Start in your Global.ascx.cs
AppDomain.CurrentDomain.SetData("DataDirectory", "C:\Users\Amrit\Desktop");
https://stackoverflow.com/a/1409378/2745294
https://stackoverflow.com/a/6708279/2745294
Hope this helps.

Reading an app setting value from the config file

In my solution I have 4 projects named UI,Business,Data and common.In the Data project I have an app.config file with following values
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="LibrarySystemConnection" value="server=(local);Initial Catalog=SanasaLibrarySystem;Integrated Security=True;
User ID=sa;Password=123"/>
</appSettings>
</configuration>
In a class of the Data project I am accessing the above key as follows:
connectionString = ConfigurationSettings.AppSettings["LibrarySystemConnection"];
When I run the code the connectionString retuns null.Anybody have an idea what is the wrong in the code
You should move the app.config file to your primary UI project.
Either move the app.config file to the project which generates exe or read the config from the assembley using this

Get connection string from app.config

I'm trying to make this simple call:
DataContext dc = new DataContext(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString)
And here's my app.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="MyDB" connectionString="Server=STEVEN-PC;Database=MyDB;Trusted_Connection=yes;" />
</connectionStrings>
</configuration>
But I'm getting an error: Object reference not set to an instance of an object.
It can't find the connection string. What am I doing wrong?
A common mistake is trying to read a connection string from an app.config from a referenced project instead of from the executable project (the web site or .exe project). You may need to copy the config settings containing the connection string to your main config file.
Double check the existence and contents of your configuration file in the build directory. That code should work fine if the config file is in place.
You could also pull put the connection string into a local variable so you can be sure the null reference exception is happening where you think it is.
You might need to specify the providerName:
<add name="MyDB" connectionString="Server=STEVEN-PC;Database=MyDB;Trusted_Connection=yes;" providerName="System.Data.SqlClient" />

A Visual Studio 2008 automated tests project can read a configuration file like app.config? (C# .NET)

I have an app.config file inside my TestProject, but when I try to read it using ConfigurationManager it reads from somewhere else and it's none of my app.config's. How to correct this?
Current config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Production" connectionString="Server=127.0.0.1,2345;Uid=user;Pwd=password;Initial Catalog=DATABASE_DATA"/>
</connectionStrings>
</configuration>
Current code:
ConfigurationManager.ConnectionStrings[0].ConnectionString
Expected output:
"Server=127.0.0.1,2345;Uid=user;Pwd=password;Initial Catalog=DATABASE_DATA"
Actual output:
"data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
Try Referencing it by name.
ConfigurationManager.ConnectionStrings["Production"].ConnectionString
The config files automatically integrate machine.config which has that SQLEXPRESS connection string by default.

Categories

Resources