Re: DirectorySearcher.FindAll() causes Unspecified Error in C# but not in VB.NET

From: Marc Scheuner [MVP ADSI] (m.scheuner_at_inova.SPAMBEGONE.ch)
Date: 10/25/04


Date: Mon, 25 Oct 2004 08:57:46 +0200


>public string getEmail(string LDAPPath, string username)
>{
> DirectoryEntry entry = new DirectoryEntry(LDAPPath);
>
> DirectorySearcher mySearcher = DirectorySearcher(entry);
>
> mySearcher.PropertiesToLoad.Add("mail");
> mySearcher.Filter =
>("&(objectClass=user)(sAMAccountName="+username+")");
>
> StringBuilder sb = new StringBuilder();
>
> foreach (SearchResult result in mySearcher.FindAll())
> sb.Append(result.Properties["0"].ToString());

First of all, it should be

                sb.Append(result.Properties[0].ToString());

If you have the 0 in double quotes, you're trying to access a property
by the name of "0" which doesn't exist, so you get back a NULL value,
which you try to cast by calling .ToString() - but you're calling that
on a NULL value, hence the error.

Also - if that particular user's "mail" attribute is NOT set, then
you'll get back a NULL value for the .Properties[0], too, which you
can't just call a .ToString() on either.

VB.NET probably shields you from those programming errors by some
means of a default behaviour - in C#, you need CHECK for those NULL
values!

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch



Relevant Pages

  • Re: Databinding to a combo box: Newbie
    ... Override the ToString function to return the 'look' that you want the ... The combobox will display the ToString output but the Selected item ... public struct stTuple ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ToString Debug vs Display strategies & preferences
    ... IFormattable implementation as well as ToString() override. ... public string FirstName ... public string ToString(string fmt, IFormatProvider fp) ... How about generalized debugging ToString stratagies? ...
    (microsoft.public.dotnet.languages.csharp)
  • Calling web service using SOAP
    ... I have created a web service which works fine when calling it from C#. ... In this I have a web method called ... public string GetMember ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: this.toString
    ... > I am a java beginner. ... > keyword makes an implicit call to Objects toString method as in the ... could have picked public String converThisObjectToAString() but I guess they ...
    (comp.lang.java.programmer)
  • Re: property inference
    ... public string FirstName ... public override string ToString() ... if you do this type of inference there's no access to the ...
    (microsoft.public.dotnet.languages.csharp)