element allowed".">

How to Fix: Only One <configSections> Element Error in Web.Config

Introduction

Today I was working on Entity Framework and trying to add the connection string to the Web.Config to specify the database. I wrote the connection string like this:

  1. <?xml version="1.0" encoding="utf-8"?>   
  2.   
  3. <configuration>    
  4.    <connectionStrings>   
  5.       <add name="SQLConnect" 
  6.            connectionString="Data Source=SAHIL; Initial Catalog=Demo; Integrated Security=SSPI" 
  7.            providerName="System.Data.SqlClient" />   
  8.    </connectionStrings>     
  9.   
  10.    <configSections>   
  11.       <sectionnamesectionname="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, 
  12.           Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
  13.    </configSections>  
  14.     
  15.    :   
  16.    :   
  17.    :   
  18.    :   
  19.    :   
  20.    :   
  21.    :   
  22. </configuration>  
When I ran the application, I experienced a strange error that says: "Only one <configSections> element allowed. It must be the first child element of the root <configuration> element".

error

It took me some time to determine the cause of the error and how to fix it.

Error:

"Only one <configSections> element allowed. It must be the first child element of the root <configuration> element".

If you read the error carefully, it states that only one <configSections> element is allowed inside the Web.config and it should be the first child element and placed at the top. The reason for the error is that I accidentally placed the <connectionStrings></connectionStrings> at the top over the <configSections></configSections> and by conventions this is a violation. So, to fix the error, I rearranged the elements and the error was fixed.
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <configuration>   
  3.   
  4.    <configSections>   
  5.       <sectionnamesectionname="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, 
  6.        Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
  7.    </configSections>  
  8.   
  9.    <connectionStrings>   
  10.       <add name="SQLConnect" 
  11.            connectionString="Data Source=SAHIL; Initial Catalog=Demo; Integrated Security=SSPI" 
  12.            providerName="System.Data.SqlClient" /> 
  13.    </connectionStrings>     
  14.   
  15.    :   
  16.    :   
  17.    :   
  18.    :   
  19.    :   
  20.    :   
  21.    :   
  22. </configuration>   
Conclusion

Your feedback and constructive criticism is always appreciated, keep it coming. Until then try to put a ding in the Universe.

 

Up Next
    Ebook Download
    View all
    Learn
    View all