Jonathan Prall Jonathan Prall - 2 months ago 28
ASP.NET (C#) Question

How to populate a gridview from Active Directory in Visual C#

I'm currently using Visual C# with ASP.NET framework, and I am trying to populate a GridView via a

DataTable
. The information is being obtained from Active Directory.

My gridview is being declared like this:

<asp:GridView ID="grdvList" runat="server" AutoGenerateColumns="False" Width="567px">
<Columns>
<asp:BoundField HeaderText="Name" ReadOnly="True" />
<asp:BoundField HeaderText="Phone" ReadOnly="True" />
<asp:BoundField HeaderText="Email" ReadOnly="True" />
</Columns>
</asp:GridView>


And my code to attempt to fill the gridview is the following:

DirectoryEntry entry = new DirectoryEntry(ConfigurationManager.AppSettings["LDAP"]);

DirectorySearcher search = new DirectorySearcher(entry)
{
SearchScope = SearchScope.Subtree,
Filter = "(&(objectClass=user)(physicalDeliveryOfficeName=Dartmouth))"
};

search.PropertiesToLoad.Add("sAMAccountName");

SearchResultCollection result = search.FindAll();

DataTable table = new DataTable();
DataRow dr = null;

//Add columns to DataTable
table.Columns.Add("Name", System.Type.GetType("System.String"));
table.Columns.Add("Phone", System.Type.GetType("System.String"));
table.Columns.Add("Email", System.Type.GetType("System.String"));

foreach (SearchResult sr in uList)
{
dr = table.NewRow();

DirectoryEntry DE = sr.GetDirectoryEntry();
dr["Name"] = DE.Properties["givenName"].Value.ToString();
dr["Phone"] = DE.Properties["telephoneNumber"].Value.ToString();
dr["Email"] = DE.Properties["mail"].Value.ToString();

table.Rows.Add(dr);
}

table.AcceptChanges();

grdvList.DataSource = table;
grdvList.DataBind();


Currently when I run it, it throws an


Object reference not set to an instance of an object error


at this line:

dr["Phone"] = DE.Properties["telephoneNumber"].Value.ToString();


Any help is definitely appreciated!

Win Win
Answer

In your scenario, you could avoid using DataTable and DataSet which are old technology. These days, we try not to use them, unless we do not have any other choice.

For NullReferenceException, you want to make sure DE.Properties["mail"] is not null before getting its Value.

For example,

DirectoryEntry entry = new DirectoryEntry(ConfigurationManager.AppSettings["LDAP"]);
DirectorySearcher search = new DirectorySearcher(entry)
{
    SearchScope = SearchScope.Subtree,
    Filter = "(&(objectClass=user)(physicalDeliveryOfficeName=Dartmouth))"
};
search.PropertiesToLoad.Add("sAMAccountName");
SearchResultCollection result = search.FindAll();

var users = result.Cast<SearchResult>().Select(sr => sr.GetDirectoryEntry())
    .Select(de => new 
    {
        Name = de.Properties["Name"] != null ? de.Properties["Name"].Value.ToString() : "",
        Phone = de.Properties["Phone"] != null ? de.Properties["Phone"].Value.ToString() : "",
        Email = de.Properties["Email"] != null ? de.Properties["Email"].Value.ToString() : "",
    }).ToList();

grdvList.DataSource = users;
grdvList.DataBind();