Anonymous
Anonymous

Reputation: 108

exception information : Message: An item with the same key has already been added

I got an An item with the same key error when i load the page.Is there any solution to solve this ? Here's the error

General Information

 - - - - - - - - - - - - - - - - - - - - - - - 

 MachineName: WIN-SKEC08HPAEB  FullName: InnoArk.APDMS.Common,
 Version=1.0.0.0, Culture=neutral, PublicKeyToken=null  AppDomainName:
 /LM/W3SVC/2/ROOT-1-130120289473554687  ThreadIdentity: innoark 

 1) Exception Information 
 - - - - - - - - - - - - - - - - - - - - - - -  Exception Type: System.ArgumentException  Message: An item with the same key has
 already been added.  ParamName: NULL  Data:
 System.Collections.ListDictionaryInternal  TargetSite: Void
 ThrowArgumentException(System.ExceptionResource)  HelpLink: NULL 
 Source: mscorlib 

 StackTrace Information 
 - - - - - - - - - - - - - - - - - - - - - - -  at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) 
 at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue
 value, Boolean add)  at
 System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value) 
 at InnoArk.APDMS.WebApp.Controllers.SimulationController._List(Guid
 GroupId, Guid bcTempId, String[] id, String[] volumeAnn, String[]
 volumeAvg, String[] costAvg, String[] costAnn, String[] costIfAvg,
 String[] costIfAnn, Boolean isEditBC, Boolean isBC, Boolean
 isSimulate, Boolean isSave) in
 D:\dev\APDMS\Trunk\InnoArk.APDMS.WebApp\Controllers\SimulationController.cs:line
 2478

Sorry , Here's the code from SimulationController from line 2478 :

detailList = new Dictionary<string, object>();
                    detailList.Add("id", entityId[j]);
                    detailList.Add("volAnn", volAnn[j]);
                    detailList.Add("volAvg", volAvg[j]);
                    detailList.Add("volLatestAnn", volLatestAnn[j]);
                    detailList.Add("volLatestAvg", volLatestAvg[j]);
                    detailList.Add("cAnn", cAnn[j]);
                    detailList.Add("cAvg", cAvg[j]);
                    detailList.Add("cIfAnn", cIfAnn[j]);
                    detailList.Add("cIfAvg", cIfAvg[j]);
                    detailList.Add("code", code[j]);
                    detailList.Add("isLatest", isLatest[j]);
                    detailList.Add("isDirty", isDirty[j]);
                    detailList.Add("curr", curr[j] == null ? "" : curr[j]);
                    detailList.Add("noOfOrder", noOfOrder[j]);
                    detailList.Add("isFilter", isFilter[j]);
                    tempList2.Add(entityId[j].ToString(), detailList); << line : 2478

Thanks for trying to answer my question ? :)

Upvotes: 0

Views: 10124

Answers (2)

Shadow Wizzard
Shadow Wizzard

Reputation: 66398

This means the dictionary named tempList2 already has a key with the value in entityId[j].

Looks like something is wrong with your loop, or entityId contains duplicate values.

Assuming it's the second option, use .Distinct():

entityId = entityId.Distinct().ToList();

In case entityId is plain array and not generic list, have such code instead:

entityId = entityId.ToList().Distinct().ToArray();

Anyway to avoid a crash on those cases, you can add such validation:

if (tempList2.ContainsKey(entityId[j].ToString()))
{
    //already exists, you can show some alert here.
}
else
{
    tempList2.Add(entityId[j].ToString(), detailList);
}

Upvotes: 3

Tomas Kirda
Tomas Kirda

Reputation: 8413

Looks like you are adding something into generic dictionary and you can not have duplicate keys there.

Before adding verify if it's already there.

Upvotes: 2

Related Questions