Search code examples
c#asp.netconnection-stringconfigxdt-transform

Duplicate Connection String Error


I have a website deployed in a server. One day it threw an error saying the connection string name is already added. I checked the web.config file and it has only one entry in that name. I removed the entry from the config. Now the website worked well and fetched data from database.

Note: When I changed the name of the config file it show error.

I think, the issue is – the connectionstring part is cached in memory. Is it so? How can we overcome this unwanted behavior?

Config Files in Source Code

Release Config

<system.web>
 <compilation xdt:Transform="RemoveAttributes(debug)" />

</system.web>

Debug Config

<system.web>

</system.web>

REFERENCES:

  1. Issue with unwanted connection string appearing in my published web config
  2. .NET 2.0 App.Config connection strings includes unwanted SQLExpress default

Solution

  • That is not the issue - when you change the web.config file, the IIS process gets reset, so there can't be any caching involved.

    What is more probable is that there is either a parent or child directory with a web.config file that contains the same connection string name - this is causing the error.

    You can solve that in several ways:

    • Ensure there is only one web.config in the correct scope with the connection string name
    • Use the remove element:

      <connectionStrings>
       <remove name="theConnectionString" />
       <add name="theConnectionString" ... />
      <connectionStrings>
      

    Refer Encrypted config file does not apply “remove” tag in connectionStrings for a related question