>public string getEmail(string LDAPPath, string username)
>{
[quoted text clipped - 10 lines]
> 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
Dave - 25 Oct 2004 12:22 GMT
Thanks for the response Mark. I found a stupid error in the LDAP
string I was passing where I wasn't appending "LDAP":
DirectoryEntry oRootDSE = new DirectoryEntry("LDAP://RootDSE");
string LDAPPath = oRootDSE.Properties["defaultNamingContext"][0].ToString();
which I fixed. But I missed the double quotes and null possibility.
if (result.Properties["mail"][0] != null)
sb.Append(result.Properties["mail"][0].ToString());
...now works, thanks again.
> 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,
[quoted text clipped - 13 lines]
> Marc Scheuner May The Source Be With You!
> Bern, Switzerland m.scheuner(at)inova.ch
Marc Scheuner [MVP ADSI] - 26 Oct 2004 10:54 GMT
>which I fixed. But I missed the double quotes and null possibility.
>
>if (result.Properties["mail"][0] != null)
> sb.Append(result.Properties["mail"][0].ToString());
Actually, I almost think you'd have to change this to:
if (result.Properties["mail"] != null)
sb.Append(result.Properties["mail"][0].ToString());
You see - if the "mail" attribute is not set, then the
.Properties["mail"] is already null, and indexing that by [0] will
cause an exception.
Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
Jay - 08 Nov 2004 20:19 GMT
I have been searching for the solution to the same error,
but I think it may be related to the DirectoryEntry()
call.
When I explicitly specify the DirectoryEntry parameters
it works fine, when I try to use a variable (LDAPQuery)
it fails. The string is being created properly as I can
assign it to a label and have it output correctly.
Any clues?
Code:
// Create AD LDAP search objects
LDAPQuery = string.Format("\"LDAP://{0}\", \"{1}\", \"{2}
\"", domain, UsrName, UsrPass);
// This does not work
DirectoryEntry SearchRoot = new DirectoryEntry(LDAPQuery);
// This works
// DirectoryEntry SearchRoot = new DirectoryEntry
("LDAP://testdomain", "fubarjones", "Testing@9");
Jay
>-----Original Message-----
>>public string getEmail(string LDAPPath, string username)
[quoted text clipped - 34 lines]
>Bern, Switzerland m.scheuner(at)inova.ch
>.
Marc Scheuner [MVP ADSI] - 09 Nov 2004 07:39 GMT
>When I explicitly specify the DirectoryEntry parameters
>it works fine, when I try to use a variable (LDAPQuery)
>it fails. The string is being created properly as I can
>assign it to a label and have it output correctly.
>// Create AD LDAP search objects
>LDAPQuery = string.Format("\"LDAP://{0}\", \"{1}\", \"{2}
>\"", domain, UsrName, UsrPass);
What EXACTLY is in the LDAPQuery string in the end here???
>// This does not work
>DirectoryEntry SearchRoot = new DirectoryEntry(LDAPQuery);
If it doesn't work, then most likely your LDAP string is invalid /
incomplete.
Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch