locked
Display user from AD in Dropdownlist control RRS feed

  • Question

  • User2098176964 posted

    Hi!
    Maybe I should have posted this in another section in this forum.

    My goal is to display users from AD in a dropdown control where value is the sAMAccountName and the text property is the DisplayName

    Error
    Index is out of interval. It can't be negative and must be less......

    aspx

     <asp:DropDownList ID="ddlEmployee" runat="server"></asp:DropDownList>

    aspx.vb

     Dim srvSearch As ADusers = New ADusers

    ddlEmployee.DataSource = srvSearch.getAllUsers()

    ddlEmployee.DataBind()

    Class

    search.Filter = "(&(objectClass=user)(objectCategory=person)(physicaldeliveryofficename=Nybro))"

    search.PropertiesToLoad.Add("samaccountname")

    search.PropertiesToLoad.Add("DisplayName")

    search.Sort.PropertyName = "DisplayName"

    Dim Results As SearchResultCollection

    Results = search.FindAll()

    Dim ddl As New DropDownList

    For Each Result As SearchResult In Results

    ddl.Items.Add(New ListItem(Result.Properties("samaccountname")(0), Result.Properties("DisplayName")(1)))

    Next

    Return ddl

     

    Tuesday, June 8, 2010 10:16 AM

Answers

  • User2098176964 posted

    Hi!

    I've changed it to a Hashtable insted af an array, and this is the result

    My error is:
    Return Hash "Returns 4 count" and varible i is on the 5:e count then it returns

    Index was outside the range. It may not be negative and must be smaller than the size of the ...
    Parameter name: index

    This is solved, solution was to remove DisplayName and add 

    result.Properties("givenName")(0).ToString() & " " & result.Properties("sn")(0).ToString()

     

    Dim srvSearch As ADusers = New ADusers

     

    ddlEmployee.DataSource = srvSearch.getAllUsers()

    ddlEmployee.DataValueField = "Key"

    ddlEmployee.DataTextField =

    "Value"

    ddlEmployee.DataBind()

     

    .vb

     

     

     

     

     

     

     

     

    search.PropertiesToLoad.Add(

    "samaccountname")

    search.PropertiesToLoad.Add(

    "DisplayName")

     

     

     

     

    Dim hash = New Hashtable

     

    Dim result As SearchResult

     

    Dim resultCol As SearchResultCollection = search.FindAll()

     

    For i As Integer = 1 To resultCol.Count

    result = resultCol(i - 1)

    hash.Add(result.Properties(

     

    "samaccountname")(0).ToString(), result.Properties("DisplayName"

    )(0).ToString())

     

    Next

     

     

    Return hash

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 10, 2010 4:18 AM

All replies

  • User1224194097 posted

    Try this:

    public void GetUsersList()
    {
     string _path = "LDAP://DomainName";
                        string _filterAttribute = UserName;
                        DirectorySearcher dSearch = new DirectorySearcher(_path);
                        // If the user enters string
                        if (!UserName.Contains("."))
                        {
                            dSearch.Filter = "(&(objectClass=user)(objectcategory=person)(displayName=" + "*" + _filterAttribute + "*))";
                        }
                        // If the user enters the FirstName.LastName
                        if (UserName.Contains("."))
                        {
                            dSearch.Filter = "(&(objectClass=user)(objectcategory=person)(SAMAccountName=" + _filterAttribute + "))";
                        }
                        // Display Name: Last Name,First Name
                        foreach (SearchResult sResultSet in dSearch.FindAll())
                        {                        
                            GetProperty(sResultSet, "displayname"),
                            GetProperty(sResultSet, "cn")
                            
                        }
    
    }
    
    
            public static string GetProperty(SearchResult searchResult, string PropertyName)
            {
                if (searchResult.Properties.Contains(PropertyName))
                {
                    return Convert.ToString(searchResult.Properties[PropertyName][0]);
                }
                else
                {
                    return string.Empty;
                }
            }


     

    Tuesday, June 8, 2010 1:07 PM
  • User2098176964 posted

    Hi Sansan!

    Thanks!, but I can't see how this replys to my post. Can you take it further?

     

    Wednesday, June 9, 2010 7:31 AM
  • User2098176964 posted

    Hi!

    I've changed it to a Hashtable insted af an array, and this is the result

    My error is:
    Return Hash "Returns 4 count" and varible i is on the 5:e count then it returns

    Index was outside the range. It may not be negative and must be smaller than the size of the ...
    Parameter name: index

    This is solved, solution was to remove DisplayName and add 

    result.Properties("givenName")(0).ToString() & " " & result.Properties("sn")(0).ToString()

     

    Dim srvSearch As ADusers = New ADusers

     

    ddlEmployee.DataSource = srvSearch.getAllUsers()

    ddlEmployee.DataValueField = "Key"

    ddlEmployee.DataTextField =

    "Value"

    ddlEmployee.DataBind()

     

    .vb

     

     

     

     

     

     

     

     

    search.PropertiesToLoad.Add(

    "samaccountname")

    search.PropertiesToLoad.Add(

    "DisplayName")

     

     

     

     

    Dim hash = New Hashtable

     

    Dim result As SearchResult

     

    Dim resultCol As SearchResultCollection = search.FindAll()

     

    For i As Integer = 1 To resultCol.Count

    result = resultCol(i - 1)

    hash.Add(result.Properties(

     

    "samaccountname")(0).ToString(), result.Properties("DisplayName"

    )(0).ToString())

     

    Next

     

     

    Return hash

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 10, 2010 4:18 AM