title: Find all users in Ldap using asp.net c#
body: <p>Below is my code. I want to retrieve all the users whose first name or last name is same in a grid. But here am getting the name itself in the grid sorted.</p>
  
  <p>I'm giving user a choice to enter user name. Once he enter the name I should be able to search in Active Directory and return all user starting with that text entered by the user.</p>
  
  <p>I should be able to display all possibilities, for example if user enters <code>adam</code> I should give him choice to select whether he want to see <code>adam josef</code> or <code>adam john</code> e.t.c.</p>
  
  <p>Any suggestion will be helpful.</p>
  
  <p>Here is the code</p>
  
  <pre><code>       DirectoryEntry de = new DirectoryEntry("ADConnection");
  
          DirectorySearcher deSearch = new DirectorySearcher(de);
  
          //set the search filter    
          deSearch.SearchRoot = de;
          String UserName = txt_To.Text;
          deSearch.Filter = "(&amp;(objectCategory=user)(GivenName=*" + UserName + "*))";
          string[] arrPropertiesToLoad = { "sn" };
          deSearch.PropertiesToLoad.AddRange(arrPropertiesToLoad);
  
        SearchResultCollection sResultColl = deSearch.FindAll();//Getting undefined error
  
  
  
          Gridview1.DataSource = sResultColl ;
          Gridview1.DataBind();
  </code></pre>
  
  <p>Here is the stack trace</p>
  
  <blockquote>
    <p>[COMException (0x80004005): Unspecified error]<br>
    System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +439513<br>
    System.DirectoryServices.DirectoryEntry.Bind() +36<br>
    System.DirectoryServices.DirectoryEntry.get_AdsObject() +31<br>
    System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) +78<br>
    System.DirectoryServices.DirectorySearcher.FindAll() +9<br>
    Certificate.WebForm4.btngo0_Click(Object sender, EventArgs e) in
    C:\Users\273714\documents\visual studio
    2010\Projects\Certificate\Certificate\WebForm4.aspx.cs:202<br>
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118<br>
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String
    eventArgument) +112<br>
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
    eventArgument) +10<br>
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
    sourceControl, String eventArgument) +13<br>
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
    +36    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    +5563</p>
  </blockquote>