Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2004

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dave - 23 Oct 2004 12:26 GMT
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#?
Marc Scheuner [MVP ADSI] - 25 Oct 2004 07:57 GMT
>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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.