"An item with the same key has already been added" in the dictionary

I have a problem with C # code. I keep getting the error "Item with the same key has already been added" and I have tried various things suggested on the net but I cannot get rid of it. Can someone help. I am getting an error on this line

ExistIncInsList.Add(WSIncInstOutput[Insrow][0], Int32.Parse(WSIncInstOutput[Insrow][1]));

      

below is the piece of code that includes 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++;
}

      

+3


source to share


1 answer


Check the correct key:

if (!ExistIncInsList.ContainsKey(WSIncInstOutput[Insrow][0]))

      



instead:

if (!ExistIncInsList.ContainsKey(WSInsName))

      

+11


source







All Articles