views:

545

answers:

4

Hi,

I have a solution with multiple projects. In each project I've added a .config file with settings that affect the local settings.

When I tried to read the settings for the database connection, I got some values which I don't know where they come from.

This is the .config file of DatabaseLayer Solution:

<configuration>
<configSections>
</configSections>
<connectionStrings>
    <add name="localDBConnection"
        connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Niko\Documents\Visual Studio 2008\Projects\GaitLinkServer\DatabaseLayer\GaitLinkDB.mdf;Integrated Security=True;User Instance=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>

When I executed the line

private string connectionString = ConfigurationManager.ConnectionStrings["localDBConnection"].ToString();

I've got the null reference object exception. So I tried to execute the following command:

ConfigurationManager.ConnectionStrings[0]

and it returned

{data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
base {System.Configuration.ConfigurationElement}: {data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
ConnectionString: "data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
Name: "LocalSqlServer"
ProviderName: "System.Data.SqlClient"

Which don't know where does it come from.

I would really appreciate if anyone could show how to make a global .config file for a solution with multiple projects and how to link local (Project) .config files.

Thanks!

+1  A: 

A given AppDomain (not a process, actually), always reads configuration data from a single file. By default, this is applicationname.EXE.CONFIG for a desktop application, or web.config for an asp.net application. No .DLL.CONFIG is ever used. You will need to copy any settings from such a file into the real configuration file.

John Saunders
A: 

All of your config settings need to go in the app.config (or web.config) for the executable project (your console app, website, etc.).

If you put config settings in the app.config for a library (DLL) project, they will not be found. You need to copy all the settings into the .config for the executable. Or you can mess with the build action for your DLL project app.configs, then try to reference those from your executable app.config. Usually it's easiest to just put all the settings in one place though.

Andy White
+1  A: 

As a note, if you use ApplicationSettings, the properties get defaults from whatever App.Config file was part of that particular project. You can specify the Application Settings in the running application, which will override the defaults.

To explain..

I have a project called CatalogData. Using the Project properties screen / Settings tab, I added a property called test, with value of test.

The Application Configuration file contains

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

...

<configSections>
    <sectionGroup name="userSettings" 
     type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
    >
        <section name="CatalogModel.Properties.Settings"
         type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
         allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
</configSections>

<userSettings>
    <CatalogModel.Properties.Settings>
        <setting name="test" serializeAs="String">
            <value>test</value>
        </setting>
    </CatalogModel.Properties.Settings>
</userSettings>

...

</configuration>

The code generated to read the setting (make it available via My.Settings.test for VB.NET or as follows in C#

CatalogModel.Properties.Settings s = new CatalogModel.Properties.Settings();
string test = s.test;

Gets defaulted in the generated code, which effectively makes the app.config file unnecessary for deployment so long as the compiled settings are OK.

In this snippet, you can see the attribute which gives the default, which is compiled.

...
    [global::System.Configuration.DefaultSettingValueAttribute("test")]

    public string test {
        get {
            return ((string)(this["test"]));
        }
    }
Greg Ogle
A: 

One thing not mentioned to the answers give so far is that any assembly running in the ASP.NET or WinForms app will recognize the application's web.config/app.config via the ConfigurationManager. So, as suggested, when you put the config info into the app's config file, the assemblies used in the app can read those values. The only thing you have to do (and document as good practice), is make sure the app/web config has a named element that the .DLL expects.

For example, you have a .DLL in a WinForms app that expects the application hosting the .DLL to have an entry in named "MyAppConn". Your DLL's code would use:

Dim cs as String = ConfigurtionManager.ConnectionStrings("MyAppConn").ToString

The DLL can be used in many different hosting applications, and as long as MyAppConn is defined in the hosting app's .config file, the DLL will get the connection string. MyAppConn can point to different databases in each hosting app, if desired.

HardCode