3

I added System.Configuration.dll as a reference. When I try to get a connection string by name, I get a null reference exception. So I tried to see if it has any using the following code:

for (int i = 0; i < ConfigurationManager.ConnectionStrings.Count; i++)
{
    string ad = ConfigurationManager.ConnectionStrings[0].ConnectionString;
}

I found out it has 3 connection strings! I am not sure from where, because I have only one in my config file, and none of these three are the one I added.

Next, I checked my appDomain:

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

I've read that file inside my application; it is exactly the same as my AppConfig file, and contains my connection string.

Why doesn't configuration manager find it, and what can I do to fix it?

3
  • are you running one or several projects under your solution Commented Dec 3, 2014 at 3:52
  • If you have multiple projects in your solution, and the project you're referring to is not the startup project, then the issue is most likely the app.config file associated with the startup project. This app.config should be edited to contain the connection string that you want. Commented Dec 3, 2014 at 4:20
  • multiple projects in solutions all win forms application, all with app config files containing connection string, not necesarly the same. I've now tried in new solution with only one project and it's working when I copy and replace that app config with one from solution. But I'm quite sure they were the same... Commented Dec 3, 2014 at 9:11

1 Answer 1

1

Make sure you are using the correct config file for your build.

Ie: If you are debubbing, Visual Studio can have an app/web.config for your debug and another for release. So make sure you are looking at the correct one.

Sign up to request clarification or add additional context in comments.

1 Comment

My project doesn't have app.config, it only has .csproject file , its a C# class library project, any idea??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.