"An item with the same key has already been added" in dictionary
.net, c#, dictionary
Solution
Check the correct key:
if (!ExistIncInsList.ContainsKey(WSIncInstOutput[Insrow][0]))
instead of:
if (!ExistIncInsList.ContainsKey(WSInsName))
Problem
I have a a problem in my C# code. I keep getting an error "An item with the same key has already been added" and I tried various things suggested on the net, but I can't seem to get rid of it. Can someone please help. I get the error on this line ``` ExistIncInsList.Add(WSIncInstOutput[Insrow][0], Int32.Parse(WSIncInstOutput[Insrow][1])); ``` below is the part of the code that involves this. Karabo ``` Dictionary<string, int> ExistIncInsList = new Dictionary<string, int>(); for (int Insrow = 2; Insrow < WSIncInstOutput.Count(); Insrow++) { int existincWSInsID = Int32.Parse(WSIncInstOutput[Insrow][1]); if (!ExistIncInsList.ContainsKey(WSInsName)) { ExistIncInsList.Add(WSIncInstOutput[Insrow][0], Int32.Parse(WSIncInstOutput[Insrow][1])); } if (MaxIncIndID < existincWSInsID) { MaxIncIndID = existincWSInsID; if (MaxIncIndID > MaxIndID) { MaxIndID = MaxIncIndID; } } } if (ExistIncInsList.ContainsKey(WSInsName)) { WSInsID = ExistIncInsList[WSInsName]; } else { WSInsID = MaxIndID + 1; MaxIndID++; } ```