tags:

views:

116

answers:

4
foreach (Benefit bene in claim.SubClaimFolderCollection[0].BenefitsFolder.BenefitCollection) 
{
    Dictionary<string, object> searchBeneficiary = 
        new Dictionary<string,object>();

     searchBeneficiary.Add("rli_beneficiariesid", ((Guid)claimant.GetValue("rli_subclaimfolderid", true)));     
}

It gives me this error

An item with the same key has already been added while adding an item to dictionary.

+6  A: 

Are you sure that the error is coming from here? It looks like for each iteration you are creating a new dictionary, inserting a single entry into it, then discarding the dictionary. That shouldn't give this error, and it's also very unlikely to be what you intended to do.

If you want to convert a collection to a Dictionary I'd suggest using the Enumerable.ToDictionary method (requires .NET 3.5).

Mark Byers
That's what i think, there shouldn't be any problem and its not actually giving any error but run time exception.
Ashutosh
@Ashutosh: Are you sure that the code you posted is the code that gives the error? Could you post the full stack trace?
Mark Byers
Sorry i can't post the whole code but yes the error is coming from this line only and i have seen it through quick watch.Also i am not using this dictionaru anywhere else in my project
Ashutosh
And if you're not using the dictionary anywhere else, why not just remove the code? It's not doing anything useful anyway.
Mark Byers
i am not putting any watch just lookin my code to see what'swrong and not following it
Ashutosh
i need this dictionary here to fetch some beneficiary data from CRM with some conditions
Ashutosh
@Ashutosh: If you need this dictionary then you are doing it wrong. As I said in my answer in your loop you are creating a new dictionary, adding a single entry to it and then *discarding it*. There is no way any other part of your code can get any reference to it. Your code is obviously wrong. Is there a reason why you don't just correct the code? Is there a reason why you aren't posting the stacktrace?
Mark Byers
StackTrace " at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)\r\n at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)\r\n at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)" string
Ashutosh
Do you not understand what Mark Byers is saying?
AHungerArtist
I understand but the thing is this is the only way to accomplish what i need to do here and it worked fine in other pieces of code but don't know wheta's wrong here i have pasted the whole stack trace for a better look for you guys.
Ashutosh
But the thing is, if the dictionary gets reset every time, aren't you performing unnecessary operations? Plus, the dictionary is in scope of the for loop so you can't access it anywhere else. How in the world can you possibly use the map if that is actually what your code looks like?
AHungerArtist
It looks like "rli_beneficiariesid" is actually being added to the dictionary twice each time through the loop (yes, I know that a new dictionary is created on each iteration, but the same key is still being added twice). See my answer.
wageoghe
Yep, this line is the error since he's calling Add("rli_beneficiariesid",...) twice. However, you're point is still valid since he's attempting to overwrite the value he just added with the void from evaluating the first Add().
The Moof
A: 

It seems that error cannot appear here: addition of a single pair to an empty Dictionary... Please, give a full stack trace from the exception.

Badiboy
StackTrace " at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)\r\n at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)\r\n at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)" string
Ashutosh
@Ashutosh: You need to provide a stack trace that **also includes the line numbers**. A stack trace without line numbers won't help much. Try running your code in debug mode. Also you will need to specify on what line number your code sample begins so that we can see which line the stacktrace refers to. Finally you should **update your question** with this information rather than posting it in comments.
Mark Byers
Stack trace did not clear the situation...
Badiboy
+1  A: 

It looks to me like the "rli_beneficiariesid" key is being added twice to the dictionary each time through the loop:

(Code copied and pasted from current code sample in original post):

searchBeneficiary.Add( 
        "rli_beneficiariesid",  
        searchBeneficiary.Add("rli_beneficiariesid", ((Guid)claimant.GetValue("rli_subclaimfolderid", true)));    

Note that searchBeneficiary.Add is being called with "rli_beneficiariesid". Inside of the Add call, searchBeneficiary.Add is being called again with the same key.

The net effect seems to be that a guid (claimant.GetValue) is being added to the searchBeneficiary dictionary with the "rli_beneficiariesid" key and then the dictionary is being added to itself with the same key!

wageoghe
sorry about that. That was a typo. Check the latest code
Ashutosh
A: 

This code cannot lead to the error. :)

Please, post the code of claimant.GetValue(...) method and from methods/properties it access from inside (if any). I suppose, that you may save smth inside this method to some other dictionary.

Badiboy