.NET Framework Developer Center >
.NET Development Forums
>
.NET Framework Setup
>
Web.config and "The entry 'KEY' has already been added"
Web.config and "The entry 'KEY' has already been added"
My connection string of my web app (.net 2.0) is in the web.config:
<connectionStrings>
<add name="KEY" connectionString="VALUE"/>
</connectionStrings>
Everything was working fine until i got this error message
System.Configuration.ConfigurationErrorsException: The entry 'KEY' has already been added. (...path...\web.config line 14)
To solve the problem, i added a <remove name="KEY"/> before the <add/>.
My question is:
The key isn't anywhere else in the project, except in the web.config.
How can it be duplicated?
Answers
.NET has several configuration files and they are all merged together. Your KEY is already defined in some other config file.
All Replies
- Could you tell us the name of the key for the collection item. Also, are you using a connection object that maps to the configuration file, on compilation time maybe it's adding the key to the list.
- The KEY is "OGPCnString".
>>are you using a connection object that maps to the configuration file,
Didn't get the question, but this is what i'm doing:
string cn = ConfigurationManager.ConnectionStrings["OGPCnString"].ConnectionString;
new SqlConnection(cn);
The exception is thrown in ConfigurationManager.ConnectionStrings["OGPCnString"].ConnectionString
Thanks. .NET has several configuration files and they are all merged together. Your KEY is already defined in some other config file.
<connectionStrings>
<remove name="KEY"/>
<add name="KEY" connectionString="Server=Manan-SQL;Database=NPSS;uid=np;pwd=np1234;connection reset=false;connection lifetime=5;enlist=true;min pool size=1;max pool size=100"/>
</connectionStrings>
Add
<remove name="KEY"/>
in your web.config as above code.
This may solve your problem. . . .

- Proposed As Answer byjaywith.7 Thursday, May 28, 2009 11:04 AM
- Hi,
Is this happen as a result of having the connection string in two places.
I have a class library and there I gave connection string in web.config since the methods in the data layer need to access DB.
(can class libraries contail web.config? If not, is it app.config?)
I have UI in a seperate web application and I add my class library as a reference for that.
Then in the code which is getting the connection string from web.config it asks for a virtual path.
I had a virtual directory for my web application (UI) so I could give a virtual path for the web.config of that web application (not the web.config in my class library)
So, finally I came up with two connection strings in both applications.
Is that the reason for that error?
If so, what is the correct place I should save the connection string?
I mean which web.config?
I'm confused with this.
Please help!
Thanks,
Jay...
So Little time; So much to do!- Edited byjaywith.7 Saturday, May 30, 2009 3:29 AM
- Is your website in a sub folder of a multiple domain hosting account? I had a similar problem and it looks like the connection string being added in the sub-domain web.config was failing because it was already being added in the root website's web.config - probably an inheritance issue.


