0

I am getting error System.ArgumentException: An item with the same key has already been added while executing following code.

The problem is Sometimes i get this error after 1 hour since program start, sometimes in 5 minutes. It depends.

I try to explain all details and couldn't find the reason for that.

 public void getWE()
    {
        if (uceTopActivity.SelectedIndex == 0)
        {
            if (dtTopAll.AsEnumerable().Any() == true)
            {
                maxDate = dtTopAll.AsEnumerable().Max(z => z.Field<DateTime>("SAMPLE_TIME"));
            }

            var isFull = (dtTopAll.AsEnumerable().Where(l => l.Field<DateTime>("SAMPLE_TIME") == maxDate && l.Field<double>("WAITS") != 0)).Any();

            if (isFull == true)
            {
                var dt1 = (from dr1 in dtTopAll.AsEnumerable()
                           where dr1.Field<DateTime>("SAMPLE_TIME") == maxDate && dr1.Field<double>("WAITS") != 0
                           group dr1 by dr1.Field<string>("Event") into g
                           select new
                           {
                               Event = g.Key,
                               WAITS = g.Sum(z => z.Field<double>("WAITS"))
                           }).ToDataTable();

                ugWaitEvent.DataSource = dt1;

                ugWaitEvent.DisplayLayout.Bands[0].Columns[1].Header.Caption = "Waits (ms/s)";
                ugWaitEvent.DisplayLayout.Bands[0].Columns[0].Width = 190;
                ugWaitEvent.DisplayLayout.Bands[0].Columns[1].SortIndicator = SortIndicator.Descending;
            }
            else
            {
                ugWaitEvent.DataSource = null;
            }

        }
    }

Here is the snapshot of the error; Error

And It has 3 exception. How can i use these exceptions tabs to get which function and which form control gets the error ;

Error: Translated - An item with the same key has already been added

Then when i search for Dictionary Word i got below lines on its Designer.cs;

global::System.Collections.Generic.Dictionary<object, global::System.Data.IDbConnection> revertConnections = new global::System.Collections.Generic.Dictionary<object, global::System.Data.IDbConnection>();

global::System.Collections.Generic.IDictionary<global::System.Data.DataRow, global::System.Data.DataRow> traversedRows = new global::System.Collections.Generic.Dictionary<global::System.Data.DataRow, global::System.Data.DataRow>();
  • 1
    Which line is throwing that exception? Are you **sure** it is the probably line? – mjwills Jun 22 '17 at 08:26
  • @mjwills .net doesn't show the line. Just throws an error like this. –  Jun 22 '17 at 08:27
  • @doctorcesar: Are you sure? What does the stack trace say? If you debug it where does it break? Its very rare to be unable to tell the line that is breaking in your own code. – Chris Jun 22 '17 at 08:46
  • @Chris I mean, it shows the line in program.cs not in the above function. and program.cs only has the line 'Application.Run(new Login());' –  Jun 22 '17 at 08:47
  • @Chris added the error ss in question –  Jun 22 '17 at 08:50
  • @mjwills Edited question with error picture. Its turkish but says 'An item with the same key has already been added' –  Jun 22 '17 at 08:52
  • While in Debug use Exceptions window to find ArgumentException and check the box next to it, so that it breaks where it's thrown. – Gerino Jun 22 '17 at 08:53
  • @Gerino yes, its checked already. –  Jun 22 '17 at 08:54
  • See https://stackoverflow.com/a/3912007/34092 and https://www.experts-exchange.com/questions/27884344/Error-An-item-with-the-same-key-as-already-been-added.html . Your SQL likely has two columns named the same thing. – mjwills Jun 22 '17 at 08:55
  • @mjwills nope. Columns are not duplicate. –  Jun 22 '17 at 08:57
  • Create a `try-catch(Exception ex)` around the `Application.Run`, catch the exception and show the stack trace by giving us the content of `ex.ToString()`. – Martin Mulder Jun 22 '17 at 10:11
  • @MartinMulder i did what you want, but still gives me same error as screenshot. –  Jun 22 '17 at 10:20
  • Perhaps. But I would like it if you give us the complete exception, including the stack trace (sorry if I was a bit vague about that). – Martin Mulder Jun 22 '17 at 10:24
  • @MartinMulder Now it gives me same error with the different function. I edited my whole question with the screen shot. –  Jun 22 '17 at 10:35
  • @Gerino Edited question. –  Jun 22 '17 at 10:38
  • Search your code for the word `Dictionary` and include the source code for any file that includes that word. – mjwills Jun 22 '17 at 10:42
  • @mjwills You want me to search for 'Dictionary' words for whole entire solution ? I did and the above function's designer have it. Then what should i do ? –  Jun 22 '17 at 10:46
  • Possible duplicate of [Argument Exception "Item with Same Key has already been added"](https://stackoverflow.com/questions/26516825/argument-exception-item-with-same-key-has-already-been-added) – mjwills Jun 22 '17 at 12:13
  • If you (temporarily) comment out the lines in the designer file starting with `DebuggerNonUserCode`, does that help you identify where the exception is occurring? – mjwills Jun 22 '17 at 21:19

1 Answers1

0

You have code that interacts with one of the following two variables:

global::System.Collections.Generic.Dictionary<object, global::System.Data.IDbConnection> revertConnections

global::System.Collections.Generic.IDictionary<global::System.Data.DataRow, global::System.Data.DataRow> traversedRows

That code attempts to add the same key more than once. See https://stackoverflow.com/a/26516900/34092 .

mjwills
  • 23,389
  • 6
  • 40
  • 63