Active Directory and ADO.NET

M

Matt

Does anyone have an example of querying Active Directory
using ADO.NET from C#? I am trying to populate a
DataTable with first name, last name and email address
but whenever I try to do this using DirectoryServices I
get an error "Object reference not set to an instance of
an object". Code as follows:

DataTable dt = new DataTable("Addresses");
dt.Columns.Add(new DataColumn("LastName",
System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("FirstName",
System.Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("Email",
System.Type.GetType("System.String")));
DirectoryEntry entry = new DirectoryEntry("MYDOMAIN.COM");

DirectorySearcher mySearcher = new
System.DirectoryServices.DirectorySearcher(entry);
mySearcher.PropertyNamesOnly = true;
mySearcher.PropertiesToLoad.Add("givenName");
mySearcher.PropertiesToLoad.Add("sn");
mySearcher.PropertiesToLoad.Add("mail");
mySearcher.Filter = ("(ObjectClass=user)");
foreach(SearchResult resEnt in mySearcher.FindAll())
{
DirectoryEntry de = resEnt.GetDirectoryEntry();
DataRow nr = dt.NewRow();
nr["LastName"] = de.Properties
["givenName"].Value.ToString();
nr["FirstName"] = de.Properties
["sn"].Value.ToString();
nr["Email"] = de.Properties["mail"].Value.ToString
();
dt.Rows.Add(nr);
}
 
M

Marc Scheuner [MVP ADSI]

I am trying to populate a
DataTable with first name, last name and email address
but whenever I try to do this using DirectoryServices I
get an error "Object reference not set to an instance of
an object". Code as follows:

DirectoryEntry entry = new DirectoryEntry("MYDOMAIN.COM");

DirectorySearcher mySearcher = new
System.DirectoryServices.DirectorySearcher(entry);
mySearcher.PropertyNamesOnly = true;
mySearcher.PropertiesToLoad.Add("givenName");
mySearcher.PropertiesToLoad.Add("sn");
mySearcher.PropertiesToLoad.Add("mail");
mySearcher.Filter = ("(ObjectClass=user)");
foreach(SearchResult resEnt in mySearcher.FindAll())
{
DirectoryEntry de = resEnt.GetDirectoryEntry();
DataRow nr = dt.NewRow();
nr["LastName"] = de.Properties
["givenName"].Value.ToString();
nr["FirstName"] = de.Properties
["sn"].Value.ToString();
nr["Email"] = de.Properties["mail"].Value.ToString
();
dt.Rows.Add(nr);
}

My gut feeling is that you're probably trying to access a
DirectoryEntry which has no value set for one of the fields you're
interested in. If that happens, that corresponding de.Properties[..]
will return a NULL pointer, which you don't check for - you just
always assume it's valid (dangerous!).

Also, you're already getting back the fields from the
DirectorySearcher - no need to do the extra step of getting the
DirectoryEntry for that SearchResult !

foreach(SearchResult resEnt in mySearcher.FindAll())
{
DataRow nr = dt.NewRow();

if(resEnt.Properties["givenName"] != null)
{
nr["LastName"] =
resEnt.Properties["givenName"].Value.ToString();
}

if(resEnt.Properties["sn"] != null)
{
nr["FirstName"] =
resEnt.Properties["sn"].Value.ToString();
}

if(resEnt.Properties["mail"] != null)
{
nr["Email"] =
resEnt.Properties["mail"].Value.ToString();
}

dt.Rows.Add(nr);
}

Of course, you could probably put the code snippet to "safely" extract
a property value into a little function of its own to make things
easier / more readable.

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top