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!