In this article I will provide solution to the following error (exception) occurring in IIS Server with ASP.Net Websites and Web Applications.
The entry 'DefaultConnection' has already been added.
This error is generally associated with the AppSetting or ConnectionStrings sections of the Web.Config file in ASP.Net.
 
 
Problem
The following error (exception) occurring in IIS Server with ASP.Net Websites and Web Applications.
The entry 'DefaultConnection' has already been added.
ASP.Net IIS Web.Config Error: The entry 'DefaultConnection' has already been added
This error is generally associated with the AppSetting or ConnectionStrings sections of the Web.Config file in ASP.Net and even though there’s no duplicate Connection String setting, occurrence of such error is quite annoying.
 
 
Cause
This error occurs when your Web Application is a child application of a Parent Website in IIS server and the Parent Website and the Child Application have the same Connection String Key name.
The reason for the above error is that by default the AppSettings, ConnectionStrings and also some other sections are inherited in the Child Applications.
 
 
Solution
There are multiple solutions to solve the above error.
1. Rename
Rename the Connection String setting Key to a different unique name so that it does not conflict with the Parent Website Connection String setting.
 
2. Clear Tag
You can also use the Clear tag ConnectionStrings section of the Web.Config which will clear all the inherited Parent Website’s Connection String settings.
<connectionStrings>
    <clear/>
    <add name="DefaultConnection" connectionString="Data Source=.\SQL2005;Initial Catalog=Northwind;Integrated Security=true"/>
    <add name="Constr" connectionString="Data Source=.\SQL2008;Initial Catalog=Northwind;Integrated Security=true"/>
</connectionStrings>
 
3. Remove Tag
The Remove Tag works same as Clear Tag the only difference is that the Clear Tag will remove all ConnectionString settings while Remove Tag will remove specific Connection String setting.
<connectionStrings>
    <remove name ="DefaultConnection"/>
    <add name="DefaultConnection" connectionString="Data Source=.\SQL2005;Initial Catalog=Northwind;Integrated Security=true"/>
    <add name="Constr" connectionString="Data Source=.\SQL2008;Initial Catalog=Northwind;Integrated Security=true"/>
</connectionStrings>
 
4. Location Tag
Finally you can use Location Tag in the Web.Config file of the Parent Website. You will need to wrap the ConnectionStrings section inside the Location tag as shown below and set the inheritInChildApplications property to false.
<location path="." inheritInChildApplications="false">
    <connectionStrings>
        <add name="DefaultConnection" connectionString="Data Source=.\SQL2005;Initial Catalog=Northwind;Integrated Security=true"/>
    </connectionStrings>
</location>
 
Note: If you are getting the following warning message in Visual Studio: The ‘inheritInChildApplications’ attribute is not allowed, then please refer my article ASP.Net Web.Config Location Tag: The ‘inheritInChildApplications’ attribute is not allowed.