index out of range

  • Thread starter Thread starter asif300
  • Start date Start date
A

asif300

Guest
please see below at the if statement I get error when mail attribute is empty//
public class Users
{
public string mail { get; set; }
public string UserName { get; set; }
public string DisplayName { get; set; }
}

static void Main(string[] args)
{

List<Users> lstADUsers = new List<Users>();
string DomainPath = "LDAP://DC=teksite,DC=local";
DirectoryEntry searchRoot = new DirectoryEntry(DomainPath);
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.Filter = "(&(objectClass=user)(objectCategory=person))";
search.PropertiesToLoad.Add("samaccountname");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("displayname");//first name
SearchResult result;
SearchResultCollection resultCol = search.FindAll();
if (resultCol != null)
{
for (int counter = 0; counter < resultCol.Count; counter++)
{
string UserNameEmailString = string.Empty;
result = resultCol[counter];
if (result.Properties.Contains("samaccountname") &&
result.Properties.Contains("displayname"))
{
Users objSurveyUsers = new Users();

objSurveyUsers.UserName = (String)result.Properties["samaccountname"][0];
objSurveyUsers.DisplayName = (String)result.Properties["displayname"][0];
if (String.IsNullOrEmpty((String)result.Properties["mail"][0])) // here I am getting Error Index out of range
{
// do nothing

}
else
{
string y = (String)result.Properties["mail"][0];

objSurveyUsers.mail = (String)result.Properties["mail"][0];
}

lstADUsers.Add(objSurveyUsers);
}
}


Console.Write(lstADUsers);


}



Console.ReadLine();

}

Continue reading...
 
Back
Top