Richard

Richard

  • NA
  • 7
  • 3.6k

An item with the same key has already been added.

Jul 9 2020 9:49 PM
I am pulling my hair out over this one I am sure it is so simple yet I cannot get it to work, I have added the below code

1        private static DynamicParameters ToDynamicParameters(this Dictionary<string, object> p)
2        {
3            DynamicParameters output = new DynamicParameters();
4
5            p.ToList().ForEach(x => p.Add(x.Key, x.Value));
6
7            return output;
8        }

When I run this and add a break point it gets to line 5 and then throws an error as per below:
System.ArgumentException: 'An item with the same key has already been added.'

this indicates that a duplicate key exists, but i cant see why as the only one that I have set for autoincrement and unique is the index value.

When I check breakpoints before I see the Values all being added correctly


The @Index is the key that gets incremented and i set the value nthg to Null so it will autoincrement due to null value. 

    int? nthg = null;

    {"@Index", nthg },
    {"@Group", tbxGroup.Text },
   
   
Everything works fine up the point it reaches the batch of code at the top to line 5 during the break point I check the and it has the vlues of x.Key and x.value as

x.key = @Index
x.value = null

I have tried removing the lines so it would add and auto increment due to them not being present however this comes up with the same error. I am up to the final step to add to sqlite but falling at the last hurdle.? I just dont see how it can be a duplicate entry because this is the very reason i set it to unique and autoincrement so when adding it should find a free number and use this as the index..

Any help very much appreciated..

Answers (3)