locked
System.ArgumentException : an item with the same key has already been added RRS feed

  • Question

  • [ArgumentException: an item with the same key has already been added]
    System.throwHelper.throwArgumentException(ExceptionResource resource) +51
    System.Collections.Generic.Dictionary'2.Insert(Tkey Key,Tvalue value,Boolean add) +7462172
    System.Web.Query.Dynamic.ClassFactory.GetDynamicClass(IEnumerable '1 properties) +122
    System.Web.Query.Dynamic.ExpressionParser.ParseNew() +406
    System.Web.Query.Dynamic.ExpressionParser.ParseIdentifier() +230
    System.Web.Query.Dynamic.ExpressionParser.ParsePrimaryStart() +34
    System.Web.Query.Dynamic.ExpressionParser.ParsePrimary() +13
    System.Web.Query.Dynamic.ExpressionParser.ParseUnary() +259

    Above written lines are the errors coming in my project. These errors are coming very rearly.

    If anyone have the solution i shall be very thankful.
    Please reply.
    Wednesday, July 8, 2009 12:10 PM

Answers


  • You're using a dictionary and you're trying to add an item with same key. Check code, avoid that.

    Dictionary.add("key1","item1")
    Dictionary.add("key2","item2")
    Dictionary.add("key1","item3") << not allowed.

    Check what part of your code is causing that.



    Thanks

    - Omie
    Someone makes my heart skip a clock cycle !
    • Edited by Omie Wednesday, July 8, 2009 12:44 PM
    • Marked as answer by Aland Li Tuesday, July 14, 2009 2:32 AM
    Wednesday, July 8, 2009 12:42 PM
  • Hi Dheer_brain,

    From my experience, the root cause is that you add an item whose key is already contained in the dictionary.

    If you want to allow the later item override the former item, please follow this:
     
    dic["key"] = "value";
     
    Otherwise, please follow this:
     
    if(dic.ContainsKey("key") == false) dic.Add("key", "value");

    Let me know if this helps.
     Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Marked as answer by Aland Li Tuesday, July 14, 2009 12:36 PM
    Friday, July 10, 2009 3:47 PM

All replies


  • You're using a dictionary and you're trying to add an item with same key. Check code, avoid that.

    Dictionary.add("key1","item1")
    Dictionary.add("key2","item2")
    Dictionary.add("key1","item3") << not allowed.

    Check what part of your code is causing that.



    Thanks

    - Omie
    Someone makes my heart skip a clock cycle !
    • Edited by Omie Wednesday, July 8, 2009 12:44 PM
    • Marked as answer by Aland Li Tuesday, July 14, 2009 2:32 AM
    Wednesday, July 8, 2009 12:42 PM
  • Hi Dheer_brain,

    From my experience, the root cause is that you add an item whose key is already contained in the dictionary.

    If you want to allow the later item override the former item, please follow this:
     
    dic["key"] = "value";
     
    Otherwise, please follow this:
     
    if(dic.ContainsKey("key") == false) dic.Add("key", "value");

    Let me know if this helps.
     Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Marked as answer by Aland Li Tuesday, July 14, 2009 12:36 PM
    Friday, July 10, 2009 3:47 PM