Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an application which got hung when i tried to add items to it.

I get a runtime error:

An item with the same key has already been added.

C#
  private class ProductUpdate
        {
            public int Index = 0;
            public string ProductCode;
        }
private void UpdateProduct( List<string> account, string userEmail, 
List<Product> products )
   {
Dictionary<string, ProductUpdate> productUpdateDictionary = new Dictionary<string, ProductUpdate>();

            for ( int i = 0; i < products.Count; i++ )
            {
       productUpdateDictionary.Add( products[i].code, new ProductUpdate { Index = i, ProductCode = products[i].code } );
            }
}


I don't know how to solve this issue ,any body has any suggestion
Thanks a lot!
Posted
Updated 17-Mar-15 10:55am
v2
Comments
Maciej Los 17-Mar-15 16:51pm    
"What did i do wrong?" You're trying to add value which already exists. Dictionary Key can not be duplicated.

There's no bug in the code that you posted. The problem is either of conceptual kind or lies in the code that calls UpdateProduct(..).

In this line:
C#
productUpdateDictionary.Add( products[i].code, new ProductUpdate { Index = i, ProductCode = products[i].code } );
your code is populating a Dictionary. A Dictionary only ever allows unique keys to be added. One or more Products in the method-argument List<Product> products have identical codes and that's the cause of the exception here.

Edit: <> troubles
 
Share this answer
 
v3
Comments
Maciej Los 17-Mar-15 17:00pm    
Nice explained, +5!
[no name] 17-Mar-15 17:15pm    
Merci :-)
Maciej Los 17-Mar-15 17:22pm    
I thought you're a German, but you behave like a Frenchman ;)
Cheers from Poland, Maciej
[no name] 17-Mar-15 17:27pm    
hehe.. tylko dla zabawy ;)
Cheers from Hamburg - Sebastian
Maciej Los 17-Mar-15 17:31pm    
Oh no, you're Pole too ;)
Please, read my comment to the question.

A dictionary[^] does not allow to add the same value twice. You need to handle it by adding Try... Catch... Finally[^] block.
 
Share this answer
 
You have to check if a key already exists before adding a value for the key. You have to do as follows.

SQL
if (!productUpdateDictionary.Contains(products[i].code)){
    productUpdateDictionary.Add(products[i].code, new ProductUpdate { Index = i, ProductCode = products[i].code });
}
 
Share this answer
 
Comments
KRI19 17-Mar-15 17:17pm    
It just worked fine. Thanks you saved my Day
[no name] 17-Mar-15 17:23pm    
Note that this avoids the exception but it's not a universal solution: If you don't expect your Products-List to contain duplicates, you're just hiding the error here. If you're fine with filtering out the duplicates, it's alright to do it like this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900