0

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?

Chris
  • 8,527
  • 10
  • 34
  • 51
aleczandru
  • 5,319
  • 15
  • 62
  • 112
  • WebConfigurationManager.ConnectionStrings["FileShareAccessLibrary"].ToString() – Sasidharan Sep 03 '13 at 12:28
  • What type of project is this? Is there any .config file in the output directory when you build the project? – Rowland Shaw Sep 03 '13 at 12:29
  • add a tag inside your tag before other tags, and test again. – VahiD Sep 03 '13 at 12:32
  • Is your actual app.config file named "yourapplication.exe.config"? app.config is copied by Visual Studio when it compiles to match the executable name, plus ".config". – Steve B Sep 03 '13 at 12:34
  • The project is a C# class library – aleczandru Sep 03 '13 at 12:34
  • the app.config file is called app.config the project name is FileShareAccessLibrary , the solution is N.Common – aleczandru Sep 03 '13 at 12:38
  • This doesn't work because you are trying to access your application's app.config from a class library. Check out http://stackoverflow.com/questions/690313/using-app-config-with-a-class-library – Dutts Sep 03 '13 at 12:38

1 Answers1

2

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.

Teppic
  • 2,506
  • 20
  • 26
  • What if I want to make some integration tests (in a separate Class library project)? Where should I put my connection string for those integration tests? – Prokurors Sep 03 '16 at 16:33