Web.config and "The entry 'KEY' has already been added"

Answered Web.config and "The entry 'KEY' has already been added"

  • Friday, December 15, 2006 11:06 AM
     
     

    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?

All Replies

  • Tuesday, December 19, 2006 2:32 AM
     
     
    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.
  • Tuesday, December 19, 2006 10:21 AM
     
     
    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.
  • Friday, March 23, 2007 3:34 PM
    Moderator
     
     Answered

    .NET has several configuration files and they are all merged together. Your KEY is already defined in some other config file. 

  • Monday, March 26, 2007 8:18 AM
     
     Proposed

    <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 by jaywith.7 Thursday, May 28, 2009 11:04 AM
    •  
  • Thursday, May 28, 2009 11:03 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 by jaywith.7 Saturday, May 30, 2009 3:29 AM
    •  
  • Sunday, July 26, 2009 2:58 PM
     
     
    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.
  • Wednesday, February 23, 2011 12:47 PM
     
     

    Hello Joel,

    Also worth checking that you do not have the Connection String added at a higher level, e.g. the MACHINE.CONFIG file.

     

    Cheers,
    Mamood.

  • Wednesday, March 23, 2011 2:13 PM
     
     
    thanks man it worked
  • Wednesday, May 11, 2011 7:03 PM
     
     

    there may be other files in the directories with the same input, perhaps a copy of theweb.config, even under another name. You can check that too.

     

    rolo

    español / spanish

    es posible que exista otro archivo en los directorios con la misma entrada, quiza una copia del web.config, inclusive con otro nombre. Puedes verificar eso tambien.