Share via


DirectorySearcher - System.Runtime.InteropServices.COMException

Question

Thursday, July 22, 2010 11:34 AM

Hi, I'm new to using Active directory and ASP.Net in general, and was hoping someone with more experience could help me out.

I have a textbox which a user would type in a name and there is a listbox that is populated by the results of the directorySearcher.
This works fine when debugging, but on our test server i recieve the error:

The specified domain either does not exist or could not be contacted.

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: The specified domain either does not exist or could not be contacted.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[COMException (0x8007054b): The specified domain either does not exist or could not be contacted.
]
   System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +451
   System.DirectoryServices.DirectoryEntry.Bind() +36
   System.DirectoryServices.DirectoryEntry.get_AdsObject() +31
   System.DirectoryServices.PropertyValueCollection.PopulateList() +25
   System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) +92
   System.DirectoryServices.PropertyCollection.get_Item(String propertyName) +131
   System.DirectoryServices.DirectorySearcher.get_SearchRoot() +91
   System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) +33
   System.DirectoryServices.DirectorySearcher.FindAll() +9
   TrainingIntranet.WebForm31.FindRequester() in C:\TrainingIntranet\TrainingIntranet\FieldCommunications\ResearchReportsForm.aspx.vb:107
   TrainingIntranet.WebForm31.txtName_TextChanged(Object sender, EventArgs e) in C:\TrainingIntranet\TrainingIntranet\FieldCommunications\ResearchReportsForm.aspx.vb:125
   System.Web.UI.WebControls.TextBox.OnTextChanged(EventArgs e) +1237468
   System.Web.UI.WebControls.TextBox.RaisePostDataChangedEvent() +52
   System.Web.UI.WebControls.TextBox.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +7
   System.Web.UI.Page.RaiseChangedEvents() +117
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1646

Here is my code:


lstNames.Items.Clear()

        Dim nameArr(2) As String
        Dim first As String
        Dim last As String
        Dim filter As String
        Dim count As Integer = 0

        nameArr = txtName.Text.Split(CChar(" "))
        first = nameArr(0)
        If nameArr.Length = 1 Then
            filter = "(&(objectCategory=person)(objectClass=user)(givenName=" + first + "))"
        Else
            last = nameArr(1)
            filter = "(&(objectCategory=person)(objectClass=user)(givenName=" + first + ")(sn=" + last + "))"
        End If

        Dim search As DirectorySearcher = New DirectorySearcher(filter)

        For Each result As SearchResult In search.FindAll

            Dim entry As DirectoryEntry = result.GetDirectoryEntry()

            foundNames(0, count) = CStr(entry.Properties("givenName").Value)
            foundNames(1, count) = CStr(entry.Properties("sn").Value)
            foundNames(2, count) = CStr(entry.Properties("mail").Value)
            foundNames(3, count) = CStr(entry.Properties("telephonenumber").Value)


            lstNames.Items.Add(foundNames(0, count) + " " + foundNames(1, count))
            count = count + 1
        Next

Any help would be greatly appreciated.

Tom

All replies (1)

Thursday, July 22, 2010 4:31 PM âś…Answered

If you want to search for users in a domain, you should provide the path for the domain like in my example below

Dim de As DirectoryEntry = new DirectoryEntry("LDAP://company.com:389/dc=company,dc=com")
Dim search As DirectorySearcher = New DirectorySearcher(de)
search.Filter = filter

More examples and info about DirectorySearcher can be obtained on MSDN.