C# .NET - Connection String in App.Config ? - Asked By SP on 17-Jun-13 05:02 PM

Hi Friends,

I have an application which uses another project for data libraries. Those libraries are common and can be used in other projects as well, So I do not want to initialize the connection string in Web.config of the main web application.

I tried it saving in App.Config file of the library project, but when I am trying to use it, it is giving me an error. I am always getting that value as NULL.

Here is how I am trying to fetch the connection string from App.Config:

public static string CONNECTING_STRING = ConfigurationManager.ConnectionStrings["dbSql"].ConnectionString;

And here is my App.Config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="dbSql" connectionString="Data Source=dbserver;Initial Catalog=TestDB;User ID=test;Password=test123;" />
  </connectionStrings>
  <appSettings>
    <add key="DefaultConnection" value="dbSql" />
  </appSettings>
</configuration>


Can anyone help me getting that issue?

Thanks in advance..
Robbe Morris replied to SP on 17-Jun-13 08:19 PM
I never make assemblies dependent upon config files.  I always set them up to force the "client" (for lack of a better word) of the assembly to pass in the necessary configuration information.

That said, I think you'll need to use ConfigurationManager.OpenExeConfiguration("full path and filename for app.config") to load the app.config file in this instance.