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:

<?xml version="1.0" encoding="utf-8"?>   
  
<configuration>    
   <connectionStrings>   
      <add name="SQLConnect" 
           connectionString="Data Source=SAHIL; Initial Catalog=Demo; Integrated Security=SSPI" 
           providerName="System.Data.SqlClient" />   
   </connectionStrings>     
  
   <configSections>   
      <sectionnamesectionname="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, 
          Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
   </configSections>  
    
   :   
   :   
   :   
   :   
   :   
   :   
   :   
</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.

<?xml version="1.0" encoding="utf-8"?>    
<configuration>   
  
   <configSections>   
      <sectionnamesectionname="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, 
       Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
   </configSections>  
  
   <connectionStrings>   
      <add name="SQLConnect" 
           connectionString="Data Source=SAHIL; Initial Catalog=Demo; Integrated Security=SSPI" 
           providerName="System.Data.SqlClient" /> 
   </connectionStrings>     
  
   :   
   :   
   :   
   :   
   :   
   :   
   :   
</configuration>   

Conclusion

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


Similar Articles