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

I am trying to bind a hashtable to dropdopwlist as below but I get this:
"Item has already been added. Key in dictionary: 'Name'  Key being added: 'Name'" Error.
 Any ideas?

    Hashtable docs = new Hashtable();
    foreach (string s in Roles.GetUsersInRole("Physician"))
    {
      ProfileCommon p = (ProfileCommon)ProfileCommon.Create(s);
      string fullname = p.PersonalInfo.FirstName + " " + p.PersonalInfo.LastName;
      docs.Add("Name", fullname);
      docs.Add("UserName", p.UserName);
    }
    ddlPhysician.DataSource = docs;
    ddlPhysician.DataTextField = "Name";
    ddlPhysician.DataValueField = "UserName";
    ddlPhysician.DataBind();

Thanks
0
sahina
1/1/2006 7:36:41 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

3 Replies
1802 Views

Similar Articles

[PageSpeed] 30
Get it on Google Play
Get it on Apple App Store

That is because you keep adding a Hashtable object with a key of "Name". Your code should look like:

   Hashtable docs = new Hashtable();

   foreach (string s in Roles.GetUsersInRole("Physician"))
   {
      ProfileCommon p = (ProfileCommon)ProfileCommon.Create(s);
      string fullname = p.PersonalInfo.FirstName + " " + p.PersonalInfo.LastName;

      docs.Add(fullname, p.UserName);
   }

   foreach (string key in docs.Keys)
   {
      ddlPhysician.Items.Add(
new ListItem(docs[key].ToString(), key));
   }


Ryan Olshan
ASPInsider | Microsoft MVP, ASP.NET
http://ryanolshan.com

How to ask a question
0
StrongTypes
1/2/2006 12:05:18 AM
Thank you, you are right...
0
sahina
1/2/2006 6:13:16 PM

On a side note, if you are using .NET 2.0, you should be using the generic collection equivalent of the Hashtable, Dictionary. This will allow for a strongly typed collection.

using System.Collections.Generic;

...

Dictionary<string, string> docs = new Dictionary<string, string>();

foreach (string s in Roles.GetUsersInRole("Physician"))
{
   ProfileCommon p = (ProfileCommon)ProfileCommon.Create(s);
   string fullname = p.PersonalInfo.FirstName + " " + p.PersonalInfo.LastName;

   docs.Add(fullname, p.UserName);
}

foreach (string key in docs.Keys)
{
   ddlPhysician.Items.Add(
new ListItem(docs[key].ToString(), key));
}


Ryan Olshan
ASPInsider | Microsoft MVP, ASP.NET
http://ryanolshan.com

How to ask a question
0
StrongTypes
1/2/2006 7:13:33 PM
Reply:

Similar Artilces:

Item has already been added. Key in dictionary: 'FitName' Key being added: 'FitName'
i am using a hashtable to keep key value pairbut i am getting an error like this Item has already been added. Key in dictionary: 'FitName'  Key being added: 'FitName'But i need a Collection which replaces the old if  we add the  same key againis there any such collection?  How about writing a custom put method like: private void putItem(HashTable myTable, string key, string value){    if( myTable.containsKey( key ) )           myTable.remove( key );   myTable.put( key, value); } Of course,...

Membership.GetAllUsers(); Item has already been added. Key in dictionary: '' Key being added: ''
 Hi, I get the following error while trying to retrive all the users in AD,Item has already been added. Key in dictionary: ''  Key being added: ''  i presume this is because a user has somehow been duplicated in th AD and the collection has to be unique. is there any method which gets all unique users as i cannot seem to find one. Thanks, Ommer Check the exist property of AD before adding user. Shah Dharnendra GSr.Analyst Programmer,GTL-Ahmedabad  Hi, is that to actually check whether a user exists in AD before adding another user? Is t...

Item has already been added. Key in dictionary: 'Id' Key being added: 'Id'
hi , when i am trying to delete a project from the project_list page i'm getting the following error : Item has already been added. Key in dictionary: 'Id'  Key being added: 'Id' can anybody help me ? thanks.Lebanese Please post your code. It will help us assess your issue.Ryan OlshanASPInsider | Microsoft MVP, ASP.NEThttp://ryanolshan.comHow to ask a question it's the time tracker starter kit code.Lebanese Charbel_Chalhoub wrote:it's the time tracker starter kit code. OK. Were you able to resolve it?Ryan OlshanASPInsider | Microsoft MVP, ASP.NEThttp://ryanolshan.comHow to a...

Item has already been added. Key in dictionary: 'cbo_' Key being added: 'cbo_'
When I use a module of a combobox, the error message is shown as below. Item has already been added. Key in dictionary: 'cbo_' Key being added: 'cbo_' Actually, in a same project, this module is also used in another webpages and it is ok but now I build a new one and the problem occurs.  How can I solve this problem? Thank you Actually, this problem occurs after I re-install windows xp and visual studio 2005. I found that the webpages wrote before are ok but the webpages after re-installation have this problem. These webpages are in the same project. After installation, I deleted...

Item has already been added. Key in dictionary: 'Cellular' Key being added: 'Cellular'
 Hi, When inserting a new item i get the following error: Item has already been added. Key in dictionary: 'Cellular'  Key being added: 'Cellular' As far as I know you get that error if you enter the parameter more than once, however i can't find anywhere in my code where the value is entered more than once. What could be the problem? Thanks in advance for the help :-) Details.aspx <asp:objectdatasource id="ObjectDataSource1" runat="server" insertmethod="InsertMomDetails" oninserted="Formview2_Inserted" oninserting="Numbers_Inserting" selectmethod="GetSingleMom" typena...

Item has already been added. Key in dictionary: '0' Key being added: '0' gridview
i'm trying to add a key and value to the hashtable and then put that hashtable in Session and this works fine but i'm getting error when i try to browse to different pages in gridview and here is the error message: Item has already been added. Key in dictionary: '0'  Key being added: '0' gridview here is my code: Hashtable ht = new Hashtable();  //declare in class level variable  protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) {        ............      &nbs...

Please hekp, error: Item has already been added. Key in dictionary: 'id' Key being added: 'id'
I had that error before on FormView, I didn't find any solution, and managed to avoid that code. But now I really need a solution on this, please help. That happens when I reload page, on my page, my FormView is loaded dynamically, and that error happens on LoadControlState and LoadViewState,  in LoadControlState, it is on             if (objArray1[5] != null)            {                  Ord...

Hashtable error
Hi,  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.<code>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("...

Refresh for second time a gridview which includes a second gridview, gives the error Item has already been added. Key in dictionary: 's_no' Key being added: '_no'
I have a gridview which includes another gridview in a template field. Included gridview is resfreshed in 'RowCreated' event of first gridview based on its' datakey values. 'Their data are refreshed by clicking a button. The first time both gridviews are filled with data without any problem. The next time I click on button for refreshing their data, I get the error Key in dictionary: 's_no'  Key being added: 's_no'. 's_no' is a datakey column of included gridview. What is the problem ?...

''''''''''''''''''''
Name: haznen Email: haznenatyahoodotcom Product: Gran Paradiso Alpha 8 Summary: '''''''''''''''''''' Comments: '''''''''''''''''''''''''''''''''''' Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) Gecko/20061204 UGES/1.7.2.0 GranParadiso/3.0a1 From URL: http://www.mozilla.org/projects/granparadiso/ Note to readers: Hendrix gives...

'''''
Name: mario Email: ramar17atfastwebnetdotit Product: Gran Paradiso Alpha 2 Summary: ''''' Comments: K: Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a2) Gecko/20070206 GranParadiso/3.0a2 ...

Key 'd' and Key 'Control'
Dear sirs / madams, I want to add a short cut key in my datawindow, the script as shown as below, Script 1 IF KeyFlags = 2 THEN IF KeyDown(Keyd!) THEN THIS.TriggerEvent("ue_deleterow") END IF END IF Script 2 IF KeyDown(KeyControl!) then IF KeyDown(Keyd!) then THIS.TriggerEvent("ue_deleterow") END IF END IF Script 3 IF KeyDown(KeyControl!) AND KeyDown(Keyd!) THEN THIS.TriggerEvent("ue_deleterow") END IF But, all of the above, if I press the Key 'Control' first, and then press Key 'd',...

Problem: The type 'typename' exists in both 'dll-name-1' and 'dll-name-2'
Hi at all!For some strange reason I sometimes (not always) get the following compilation error when starting my ASP.net project:Server Error in '/' Application. Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0433: The type 'ASP.formscontrols_worker_it_ascx' exists in both 'c:\WINDOWS\Microsoft.NET\Framework\v2.0....

'do' won't 'do' if '/'
Greetings to All from Au, Have a NetWare Perl 5.8.4 and wanted to tweak File\Spec\NW.pm to try and standardise on '/' separators. If I run a test script (t/uni/lower.t) with an unmodified NW.pm, it calls t/uni/case.pl, that, in case.pl, (when the path separators are '\'), the $file is '..\lib\unicore\To\Lower.pl' and the following code portion works, with $simple getting a returned table: sub casetest { my ($base, $spec, $func) = @_; my $file = File::Spec->catfile(File::Spec->catdir(File::Spec->updir, "lib", &qu...

Web resources about - Item has already been added. Key in dictionary: 'Name' Key being added: 'Name' - asp.net.web-forms

Resources last updated: 12/22/2015 2:02:55 AM