locked
How to add all the adam users in grid RRS feed

  • Question

  • User238037111 posted

    I want to add all the adam users in grid. I am using following code

                DirectoryEntry root = new DirectoryEntry(strPath);
                DirectorySearcher search = new DirectorySearcher(root);
                search.PageSize=1000;
                search.PropertiesToLoad.AddRange(new String[]{"username","distinguishedname"});
                SearchResultCollection oResults = search.FindAll();

                foreach (SearchResult oResult in oResults)
                {

                    DataRow dr = dt.NewRow();
                    dr["UserName"] = SearchResult.Properties["username"].ToString();
                    dr["DNName"] = "";//                dt.Rows.Add(dr);
                }

    But it is giving me error in following line

    dr["UserName"] = oResult.Properties["username"].ToString();

    It seems oResult properties have some issue, Please let me know whats wrong I am oing?

    Avi

     

    Thursday, July 23, 2009 2:51 PM

Answers

  • User-2009597737 posted

    Mail is not a mandatory AD attribute.  To check if an AD property has value you should replace this

      if (oResult.Properties["mail"][0].ToString() != "")

    With:


    if (oResult.Contains(mail))  // exists

    dr["userName"] = oResult.Properties[mail].Value.ToString();

    else

     ......





    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 24, 2009 8:53 AM

All replies

  • User-2009597737 posted

    Replace username with samAccountName to start with.  I am not sure what AddRange does, I have not had the requirement to use this method.  You can still get all user w/o setting properties to load.


    Thursday, July 23, 2009 3:35 PM
  • User238037111 posted

    I already tried, but its not working.

    Can you suggest me any code ot article using C#?

     

    Thursday, July 23, 2009 3:59 PM
  • User238037111 posted

    Here is my code

     string strPath = "LDAP://portal:389/OU=MY Users,OU=MYADAM,O=ADAM,C=US ";
     DirectoryEntry root = new DirectoryEntry(strPath);
     DirectorySearcher search = new DirectorySearcher(root);
     search.PageSize = 1000;
     SearchResultCollection oResults = search.FindAll();
     foreach (SearchResult oResult in oResults)
      {
       DataRow dr = dt.NewRow();
       if (oResult.Properties["mail"][0].ToString() != "")
       {
        dr["UserName"] = oResult.Properties["mail"][0].ToString();
       }
       else
       {
        dr["UserName"] = "n/a";
       }
       dr["CreationDate"] = "2";// user.CreationDate;
       dr["LastLoggedIn"] = "3";// user.CreationDate;
       dt.Rows.Add(dr);
      }

    If I used following line,

    dr["UserName"] = oResult.Properties["mail"][0].ToString();

    It gives me following error

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    It seems some of the users have null value in mail field. I want to display all the user doesnt matter they have value in mail or not.

    If I used following line, it works correct

    dr["UserName"] = oResult.Properties["name"][0].ToString();

    I also want that datagrid should automatically sorted on "UserName" column. I am using following code for

    oGrid.DataSource = _dv;
    oGrid.AutoGenerateColumns = false;
    oGrid.AllowPaging = true;
    oGrid.AllowSorting = true;
    oGrid.Sorting += new GridViewSortEventHandler(this.oGrid_Sorting);
    oGrid.PageSize = 10;
    oGrid.PagerSettings.Mode = PagerButtons.NumericFirstLast;
    oGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;

    Secondly Is there any way to set font of Pager Navigation?

    Avi 

    Please advise

    Thursday, July 23, 2009 4:30 PM
  • User-2009597737 posted

    Mail is not a mandatory AD attribute.  To check if an AD property has value you should replace this

      if (oResult.Properties["mail"][0].ToString() != "")

    With:


    if (oResult.Contains(mail))  // exists

    dr["userName"] = oResult.Properties[mail].Value.ToString();

    else

     ......





    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 24, 2009 8:53 AM
  • User238037111 posted

    Raghu

    Thanks,  it works nicely.

    I also resolved sorting issue of grid by adding following
    _dv.Sort = ""UserName";

    before this line
    oGrid.DataSource = _dv;

    Now I am working on setting font of Pager Navigation, let me know if you can  help me here.

    Friday, July 24, 2009 10:02 AM