The entry '' has already been added error
Asked Answered
V

4

84

I have a web.config in ASP.net giving me configuration error.

 <connectionStrings>   
<add name="conn1" connectionString="Data Source=test;database=test;uid=test;pwd=test"/>
<add name="conn2" connectionString="Data Source=123.123.1.123;database=test2;uid=test;pwd=test"/>
...

It throws "The entry 'conn2' has already been added." error. I know I only added it once. Not sure what it is wrong with.

Vomit answered 13/7, 2011 at 12:56 Comment(4)
There should be a line number that is spewed out with the error. Have you checked that it corresponds with the snippet you have above? Are you sure you haven't got another parameter named "conn2"?Glorious
I'm assuming you're using a database? MSSQL, mongoDB, something else? Have you set a unique field to uid or pwd fields in the DB? This would create a duplicate entry for that field and depending on the DB (each have their own error terminology) being used could spit this error. I hope this helpsSipple
Check you web.config, you must have defined conn2 somewhere else; otherwist it will not error out. Well I just tested adding those given connection string to my test web.config file and it didn't throw any error.Tarrsus
Yes, I checked the line number and any duplicate in the file, but couldn't find one.Vomit
M
178

it can be in web.config that is parent to this one. just add

<remove name="conn2" />

before your add it (again) :)

alternatively clear all connection string using

<clear />
Merissa answered 13/7, 2011 at 12:59 Comment(4)
Amazing - saved my day - my project started spewing errors after adding a publish profile and publishing.Freeze
In my case, I have a web.config file associated with the folder where my ASP.NET application is located, but I have created a Visual Studio project on its parent folder. When opening the Linq DBML file it ported the connection string (which was set on the child web.config file) to the root folder web.config, thus causing it to duplicate. Thank you for pointing this out.Deejay
Okay, so this is the solution/workaround. But why does it happen intermittently?Cosmonautics
There are multiple configs which are taken in order. Value with same key can be defined in config higher in order and, you can't define it again without removing it first. This is no workaround, this is solution. You can also fix it by finding config where it is defined removing it there (if you can, sometimes you don't have access to them.). More about it here: msdn.microsoft.com/en-us/library/ms178685.aspxMerissa
D
18

You should use <clear /> when adding any providers to your web.config. Read this article: http://weblogs.asp.net/scottgu/archive/2006/11/20/common-gotcha-don-t-forget-to-clear-when-adding-providers.aspx

The root cause of the above problem rests in how the new provider was registered within the web.config file.

The section within the web.config file is implemented as a collection, and so it is possible to register multiple providers at the same time

If you have another project using the same connection string name you will receive this error because that connection string has already been added to the collection.

Deoxidize answered 13/7, 2011 at 13:9 Comment(1)
The question is about connection strings, not providers, so the info you point to is off-topic. Providers utilise connection strings but aren't the same thingIlla
P
4

I got this error and the issue turned out to be a web.config deployed to the root of the domain

Putty answered 30/1, 2020 at 21:9 Comment(0)
Y
2

If you override the ToString() method then this will work. Looks like the configuration mechanism uses that to check if something already exists in the collection

Yajairayajurveda answered 29/3, 2012 at 8:50 Comment(2)
Actually, it depends on the implementation of protected override object GetElementKey(ConfigurationElement element) in the configuration element. But your answer did guide me in the correct direction so +1Cylix
To be clear, GetElementKey is on the collection element, not the configuration element.Entwine

© 2022 - 2024 — McMap. All rights reserved.