2

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!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Jeff B Jul 12 '16 at 18:21

1 Answers1

1

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();
Win
  • 61,100
  • 13
  • 102
  • 181
  • Okay, this is a step in the right direction. Only error in your code is that we need `Name = de.Properties["Name"].Value != null` instead of `Name = de.Properties["Name"] != null`. My gridview is filled with the appropriate amount of rows, however there is nothing in them... http://i.imgur.com/eVaodTy.png – Jonathan Prall Jul 12 '16 at 19:00
  • You still need **datafield** attribute for boundfield. For example, `` – Win Jul 12 '16 at 19:11
  • That worked! The only remaining issue was that in the original code you posted, you didn't use the correct LDAP names. de.properties["mail"] for instance. It now works though, thank you! – Jonathan Prall Jul 12 '16 at 19:17