0

I am Learning c#. I am trying to get the displayName from AD. Here is the code for the following.

if ( resEnt.Properties["displayname"].Count > 0)
{
    nameList = new List<string>();
    name = resEnt.Properties["displayname"][0].ToString();
    nameList.Add(name.ToString());
    int count = nameList.Count;
    Console.WriteLine("name: " + resEnt.Properties["displayname"][0].ToString());

    comboBox1.Items.Add(name.ToString());
 }

The error i am getting is

Index was out of range. Must be non-negative and less than the size of the collection.

on the line

 name = resEnt.Properties["displayname"][0].ToString();
AxdorphCoder
  • 1,104
  • 2
  • 15
  • 26

1 Answers1

0

Maybe resEnt.Properties["displayname"][0] is null? Anyway, what is resEnt? is it of type DirectoryEntry? if so, you should try resEnt.Properties["displayname"].Value.ToString()

eyalfein
  • 48
  • 1
  • 5