Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8803493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:21:15+00:00 2026-06-14T01:21:15+00:00

Below is my code. I want to retrieve all the users whose first name

  • 0

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.

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.

I should be able to display all possibilities, for example if user enters adam I should give him choice to select whether he want to see adam josef or adam john e.t.c.

Any suggestion will be helpful.

Here is the 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 = "(&(objectCategory=user)(GivenName=*" + UserName + "*))";
        string[] arrPropertiesToLoad = { "sn" };
        deSearch.PropertiesToLoad.AddRange(arrPropertiesToLoad);

      SearchResultCollection sResultColl = deSearch.FindAll();//Getting undefined error



        Gridview1.DataSource = sResultColl ;
        Gridview1.DataBind();

Here is the stack trace

[COMException (0x80004005): Unspecified error]
System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +439513
System.DirectoryServices.DirectoryEntry.Bind() +36
System.DirectoryServices.DirectoryEntry.get_AdsObject() +31
System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) +78
System.DirectoryServices.DirectorySearcher.FindAll() +9
Certificate.WebForm4.btngo0_Click(Object sender, EventArgs e) in
C:\Users\273714\documents\visual studio
2010\Projects\Certificate\Certificate\WebForm4.aspx.cs:202
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String
eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
+36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+5563

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:21:16+00:00Added an answer on June 14, 2026 at 1:21 am

    You can use a PrincipalSearcher and a “query-by-example” principal to do your searching:

    // create your domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    // define a "query-by-example" principal - here, we search for a UserPrincipal 
    // and with the first name (GivenName) of "Bruce" and a last name (Surname) of "Miller"
    UserPrincipal qbeUser = new UserPrincipal(ctx);
    qbeUser.GivenName = "*" + UserName + "*";
    
    // create your principal searcher passing in the QBE principal    
    PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
    
    // find all matches
    foreach(var found in srch.FindAll())
    {
        // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
    }
    

    If you haven’t already – absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement. Or see the MSDN documentation on the System.DirectoryServices.AccountManagement namespace.

    Of course, depending on your need, you might want to specify other properties on that “query-by-example” user principal you create:

    • DisplayName (typically: first name + space + last name)
    • SAM Account Name – your Windows/AD account name
    • User Principal Name – your “username@yourcompany.com” style name

    You can specify any of the properties on the UserPrincipal and use those as “query-by-example” for your PrincipalSearcher.

    Update: if you want to find a bunch of users and bind those to a gridview, use this code:

    // create your domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    // define a "query-by-example" principal - here, we search for a UserPrincipal 
    // and with the first name (GivenName) of "Bruce" and a last name (Surname) of "Miller"
    UserPrincipal qbeUser = new UserPrincipal(ctx);
    qbeUser.GivenName = "*" + UserName + "*";
    
    // create your principal searcher passing in the QBE principal    
    PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
    
    var results = srch.FindAll();
    
    Gridview1.DataSource = results; 
    Gridview1.DataBind();
    

    Don’t iterate twice over all the data returned! (as in your comment
    ) …..

    Update #2: with the S.DS.AM classes, you always get the complete class – there’s nothing you can do about this. If you want to select only specific LDAP attributes, you need to use your original approach.

    From that approach, you need to make sure to create the root of the DirectorySearcher on a container – e.g. the OU=Users container – NOT on an individual AD object like a specific user.

    So try using this code:

    // define a *CONTAINER* as the root of your searcher!
    DirectoryEntry de = new DirectoryEntry("LDAP://OU=Users,OU=NJY,OU=NewJersey,OU=USA,OU=NorthAmerica,OU=America,OU=gunt,DC=xxx,DC=com");
    
    DirectorySearcher deSearch = new DirectorySearcher(de);
    
    // set the search filter    
    string UserName = txt_To.Text;
    
    deSearch.Filter = string.Format("(&(objectCategory=user)(givenName=*{0}*))", UserName);
    deSearch.PropertiesToLoad.Add("sn");
    
    SearchResultCollection sResultColl = deSearch.FindAll();
    
    Gridview1.DataSource = sResultColl;
    Gridview1.DataBind();
    

    Does that work?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to open an xlsx file, I have tried the below code,but neither
The code below does not work but meant to illustrate what I want to
I want to retrieve all the html elements after font:first element inside p in
I want to retrieve all the users in my db and put them in
This is my below code and I want to catch the exception if any
See code below: I want Page 1 to alert #testing... This works in C
I want to know if the below code: <?php printf (%s, $some_variable); ?> is
I want to know is below code correct ? I have following code which
Issue In the below code , say if i want to change the color
I want to send data through URL.I've tried the below code: Intent browserIntent =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.