1

I don't even know if I can do what I'm attempting but I've imported forms from several projects and added references to those projects. Each project has a different set of connection strings and I'm trying to get them to coexist in App.config where I can filter by SECTION (Users select connections from comboboxes). I am hoping I can do this by implementing ConfigSections. If it's doable I obviously don't know how.

Attached is my App.config. I'm getting the error 'configuration system failed to initialize' and when I drill into the detail it says 'unrecognized configuration section amSettings

Is what I'm trying to do possible? If so, what do I need to correct?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="ApplicationSettings" 
                      type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="amSettings.Properties.Settings" 
                     type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
                     allowExeDefinition="MachineToLocalUser" 
                     requirePermission="false"/>
            <section name="cbSettings.Properties.Settings"
         type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
         requirePermission="false"/>
        </sectionGroup>
    </configSections>

    <amSettings>
        <add key="VX130 Attribute Map Connections" value="Sample Console Application" />
        <add key="Region 1 VX130"   value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 2 VX130"   value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 3 VX130"   value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 4 VX130"   value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="CDW"  value="Server=VHACDWA01;Database=;Trusted_Connection=true;"/>
    </amSettings>
    <cbSettings>
        <add key="CDW Class Builder Connections" value="Sample Console Application" />
        <add key="Region 1 Class Build"     value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 2 Class Build"     value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 3 Class Build"     value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 4 Class Build"     value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="DEVELOPMENT Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
        <add key="PREVIEW Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
        <add key="VERSION Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
    </cbSettings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>
Thom Ash
  • 221
  • 4
  • 20

2 Answers2

2

Change the section name from amSettings.Properties.Settings to amSettings and cbSettings.Properties.Settings to cbSettings

e.g.

    `<section name="amSettings" `

Here is a comprehensive example:

If you change your config file to this:

    <configSections>
        <section name="amSettings"
                 type="System.Configuration.AppSettingsSection"
                 allowExeDefinition="MachineToLocalUser"
                 requirePermission="false"/>
        <section name="cbSettings"
     type="System.Configuration.AppSettingsSection"
     requirePermission="false"/>
  </configSections>
  <amSettings>
      <add key="ABC" value="DEF"/>
  </amSettings>

Then you can access the key ABC using this code:

        var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection("amSettings");
        var a = appSettingSection.Settings["ABC"].Value;
user469104
  • 1,206
  • 12
  • 15
  • I tried that and get same error. Don't know if it's relevant but I can remove amSettings and cbSettings sections and just leave the ConfigSections definition and the error goes away. Unfortunately I know longer have connection strings. – Thom Ash Oct 10 '14 at 17:41
  • Your answer is part of it. Also had to wrap sections with group name. – Thom Ash Oct 10 '14 at 17:59
  • OK, did not see your comment until after my edits. If you do not explicitly want the ApplicationSettings section you can remove the section group and define the amSettings and cbSettings directly. – user469104 Oct 10 '14 at 18:11
  • Okay. Don't particularly need sectionGroup "ApplicationSettings" if I can reference sections directly. I'll try it. In any event the error is gone and I just need to figure out how to reference in my code to set SQL Connection strings. Thanks for the help. – Thom Ash Oct 10 '14 at 18:26
0

The solution was two things. Change section name as user469104 recommended and wrapping sections in Group Name.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="ApplicationSettings" 
                      type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="amSettings" 
                     type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
                     allowExeDefinition="MachineToLocalUser" 
                     requirePermission="false"/>
            <section name="cbSettings"
         type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
         requirePermission="false"/>
        </sectionGroup>
    </configSections>
<ApplicationSettings>
    <amSettings>
        <add key="VX130 Attribute Map Connections" value="Sample Console Application" />
        <add key="Region 1 VX130"   value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 2 VX130"   value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 3 VX130"   value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 4 VX130"   value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="CDW"  value="Server=VHACDWA01;Database=;Trusted_Connection=true;"/>
    </amSettings>
    <cbSettings>
        <add key="CDW Class Builder Connections" value="Sample Console Application" />
        <add key="Region 1 Class Build"     value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 2 Class Build"     value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 3 Class Build"     value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 4 Class Build"     value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="DEVELOPMENT Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
        <add key="PREVIEW Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
        <add key="VERSION Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
    </cbSettings>
</ApplicationSettings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>
Thom Ash
  • 221
  • 4
  • 20