title: C# and VB.NET LDAP Search Different?
body: <p>Does anyone know if there is a difference between the implementation of the FindAll() method on the DirectorySearcher object in C# and VB.NET? From my understanding they both get "compiled" to MSIL and get processed by the CLR the same way. Going against our ADAM/LDAP system the below C# code throws an error and the below VB.NET does not.</p>
  
  <p>Here is the C# exception stack:</p>
  
  <pre><code>at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
    at System.DirectoryServices.DirectoryEntry.Bind()
    at System.DirectoryServices.DirectoryEntry.get_AdsObject()
    at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
    at System.DirectoryServices.DirectorySearcher.FindAll()
  </code></pre>
  
  <p>Here is the C# error:</p>
  
  <pre><code>System.Runtime.InteropServices.COMException was unhandled
  Message="The parameter is incorrect.\r\n"
  Source="System.DirectoryServices"
  ErrorCode=-2147024809
  </code></pre>
  
  <p>C# code:</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
  {
      DirectoryEntry root = new DirectoryEntry("LDAP://directory.corp.com/OU=Person,OU=Lookups,O=Corp,C=US", null, null, AuthenticationTypes.Anonymous);
      DirectorySearcher mySearcher = new DirectorySearcher(root);
  
      mySearcher.Filter = "(uid=ssnlxxx)";
      mySearcher.PropertiesToLoad.Add("cn");
      mySearcher.PropertiesToLoad.Add("mail");
  
      SearchResultCollection searchResultCollection = null;
      searchResultCollection = mySearcher.FindAll(); //this is where the error occurs
  
      try
      {
          foreach (SearchResult resEnt in searchResultCollection)
          {
              Console.Write(resEnt.Properties["cn"][0].ToString());
              Console.Write(resEnt.Properties["mail"][0].ToString());
          }
      }
      catch (DirectoryServicesCOMException ex)
      {
          MessageBox.Show("Failed to connect LDAP domain, Check username or password to get user details.");
      }
  }
  </code></pre>
  
  <p>This is the VB.NET code that works:</p>
  
  <pre><code>Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
  
      Dim root As New     DirectoryEntry("LDAP://directory.corp.com/OU=People,OU=Lookups,O=corp,C=US", vbNull, vbNull, authenticationType:=DirectoryServices.AuthenticationTypes.Anonymous)
      Dim searcher As New DirectorySearcher(root)
      searcher.Filter = "(uid=ssnlxxx)"
      searcher.PropertiesToLoad.Add("cn")
      searcher.PropertiesToLoad.Add("mail")
  
      Dim results As SearchResultCollection
  
      Try
          results = searcher.FindAll()
  
          Dim result As SearchResult
          For Each result In results
              Console.WriteLine(result.Properties("cn")(0))
              Console.WriteLine(result.Properties("mail")(0))
          Next result
      Catch ex As Exception
          MessageBox.Show("There was an error")
      End Try
  End Sub
  </code></pre>