0

I got mixed with app.config file.

I had config that failed to get connectionStrings:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <configSections>
    <connectionStrings>
         <add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
         <add name="ProgrammingInCSharpConnection"  providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=ProgrammingInCSharp;" />
    </connectionStrings>
   </configSections>
</configuration>

Problem was solved after placing <connectionStrings> outside of <connectionStrings> :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <configSections />
   <connectionStrings>
       <add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
       <add name="ProgrammingInCSharpConnection"  providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=ProgrammingInCSharp;" />
   </connectionStrings>
</configuration>

But I found many samples with pattern below

<configSections>
    <connectionStrings>
       ...
    </connectionStrings>
</configSections>

It might work in some cases? What is logic of configuration file in general and what kind of information might be located in each section?

vico
  • 17,051
  • 45
  • 159
  • 315

2 Answers2

0

The configSections node contains configuration section per namespace declarations, but is not usually used for connection strings, unless they are namespace - specific.

But I found many samples with pattern below

The patterns you found could never work since <connectionStrings> is not allowed to be nested directly under <configSections>. This could be achieved, by stating a connection string variable under the <section> itself, and if the server code targets them via a specific namespace.

An example:

<configuration>
   <configSections>
      <section name="section1" type="System.MyNameSpace" />
   </configSections>
   <section1 connString="...conn.." />
</configuration>

In the example above the namespace System.MyNameSpace will now have a variable (field) called connString, so it can target it in code behind.

For conclusion, if your connection strings are not per namespace, the <connectionStrings> element should be nested directly under the <configuration> node and no where else.

Koby Douek
  • 16,156
  • 19
  • 74
  • 103
0

No. It is not possible to have a valid app.config file and yet having a connectionStrings in your configSections node.

According to MSDN, this is the list of allowed elements:

  • section
  • sectionGroup
  • add
  • clear

connectionStrings isn't in the list and I don't expect an application to find the connection strings in that place.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325