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

D

Dave

Hi

All C# ADSI samples that I run cause unspecified errors. If I
translate them into VB.NET they run fine.

Here's an example:

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());

return(sb.ToString());
}

results in:

Unspecified error
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException:
Unspecified error

Source Error:


Line 41: StringBuilder sb = new StringBuilder();
Line 42:
Line 43: foreach (SearchResult result in mySearcher.FindAll())
Line 44: sb.Append(result.Properties["0"].ToString());
Line 45:


Source File: c:\inetpub\wwwroot\area51\adsi\webform6.aspx.cs Line:
43

Stack Trace:


[COMException (0x80004005): Unspecified error]
System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
+704
System.DirectoryServices.DirectoryEntry.Bind() +10
System.DirectoryServices.DirectoryEntry.get_AdsObject() +10
System.DirectoryServices.DirectorySearcher.FindAll(Boolean
findMoreThanOne) +198
System.DirectoryServices.DirectorySearcher.FindAll() +10
area51.ADSI.WebForm6.getEmail(String LDAPPath, String username) in
c:\inetpub\wwwroot\area51\adsi\webform6.aspx.cs:43
area51.ADSI.WebForm6.Page_Load(Object sender, EventArgs e) in
c:\inetpub\wwwroot\area51\adsi\webform6.aspx.cs:28
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750

I'm using "<identity impersonate="true" />" in the web config file.
Any ideas why I can use DirectorySearcher from VB but not from C#?
 
M

Marc Scheuner [MVP ADSI]

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
 
D

Dave

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.
 
M

Marc Scheuner [MVP ADSI]

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
 
J

Jay

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)
{
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
.
 
M

Marc Scheuner [MVP ADSI]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top