Hashtable error - System.ArgumentException: Item has already been added. Key in dictionary: 'ItemNo' Key being added: 'ItemNo'


 Can someone point me in the right direction?

 I have no idea why I get this error because I am quite sure that in my code, "ItemNo" is being added as a key for the FIRST time into the Hashtable.

Hashtable htCredent = new Hashtable();

... (I have left out some chunks of code here that populates the datareader drCredent.)

while (drCredent.Read())


htCredent.Add("ItemNo", drCredent.GetString(0)); (*** ERROR Line)

htCredent.Add("ItemDescription", drCredent.GetString(1));

htCredent.Add("GST", drCredent.GetDecimal(2));

htCredent.Add("Discount", drCredent.GetDecimal(3));

htCredent.Add("UnitPrice", drCredent.GetDecimal(4));

htCredent.Add("CurrencyCode", drCredent.GetString(5));

htCredent.Add("EOS", drCredent.GetDateTime(6));

htCredent.Add("TITU", drCredent.GetDateTime(7));

htCredent.Add("CountryName", drCredent.GetString(8));

htCredent.Add("StatesName", drCredent.GetString(9));

htCredent.Add("OnSite", drCredent.GetDecimal(10));

htCredent.Add("DocName", drCredent.GetString(11));

htCredent.Add("TaxTerm", drCredent.GetString(12));

htCredent.Add("SafetyDoc", drCredent.GetString(13));

htCredent.Add("ProductLine", drCredent.GetString(14));


This is the complete error.

Item has already been added. Key in dictionary: 'ItemNo'  Key being added: 'ItemNo'

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Item has already been added. Key in dictionary: 'ItemNo'  Key being added: 'ItemNo'

Source Error:

Line 88:             {
Line 89: 
Line 90:                 htCredent.Add("ItemNo", drCredent.GetString(0));
Line 91:                 htCredent.Add("ItemDescription", drCredent.GetString(1));
Line 92:                 htCredent.Add("GST", drCredent.GetDecimal(2));

Stack Trace:

[ArgumentException: Item has already been added. Key in dictionary: 'ItemNo'  Key being added: 'ItemNo']
   System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add) +2836099
   System.Collections.Hashtable.Add(Object key, Object value) +11
   APESSO.Entry..ctor() in c:\Inetpub\wwwroot\APESSO\App_Code\Entry.cs:90
   _default.Page_Load(Object sender, EventArgs e) in c:\Inetpub\wwwroot\APESSO\default.aspx.cs:17
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061


Please help!


10/27/2006 4:35:51 AM
📁 asp.net.object-datasource
📃 16182 articles.

💬 1 Replies

How many records does your datareader contain?  You have that code in a loop, and I am assuming then that there is more than one record, which is making that line of code run more than once, causing the error you are seeing.

Terri Morton
Engagement Manager, Neudesic

How to ask a question

10/27/2006 2:25:50 PM