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
- Next message: Jon Skeet [C# MVP]: "Re: .NET Framework required for C#?"
- Previous message: promko: "re:PInvoke. Passing void* instance."
- In reply to: Dave: "DirectorySearcher.FindAll() causes Unspecified Error in C# but not in VB.NET"
- Next in thread: Dave: "Re: DirectorySearcher.FindAll() causes Unspecified Error in C# but not in VB.NET"
- Reply: Dave: "Re: DirectorySearcher.FindAll() causes Unspecified Error in C# but not in VB.NET"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Jon Skeet [C# MVP]: "Re: .NET Framework required for C#?"
- Previous message: promko: "re:PInvoke. Passing void* instance."
- In reply to: Dave: "DirectorySearcher.FindAll() causes Unspecified Error in C# but not in VB.NET"
- Next in thread: Dave: "Re: DirectorySearcher.FindAll() causes Unspecified Error in C# but not in VB.NET"
- Reply: Dave: "Re: DirectorySearcher.FindAll() causes Unspecified Error in C# but not in VB.NET"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|