473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Active Directory and ADO.NET

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);
}
Nov 22 '05 #1
1 1777
>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
Nov 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jay Chan | last post by:
We have just installed a SQL Server 2000 (SP 3A) onto a computer that has Windows-2003 Server on it. Now, we cannot get access to that database server from other computers. Seem like this may be an...
0
by: microsoft | last post by:
Hi People, when I try to modify an active directory user programatically, I receive the following exception: The server is unwilling to process the request Reading the microsoft web site, I...
9
by: Mario Rodriguez | last post by:
Hi people. I have a problem adding users to Win2003 active directory programatically. When I execute my app throws the following exception: .................The specified directory service...
4
by: ASGMikeG | last post by:
Hi, How do I find the user object for the current user in Active Directory i.e. the user running my program ? Regards Michael
1
by: Andrew | last post by:
Hey all, Working on revamping our Intranet here and making use of the LDPA, Active Directory, Directory Services, etc. that .Net provides. I am still fairly new on this subject, so the problem...
6
by: Leo_Surf | last post by:
Hello, I need your help adding user in Active Directory from ASP.net website. Could any one provide me the complete code for the html page. As this is my curriculam project and I dont have any...
1
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
10
by: Hriday | last post by:
Hi there, Please help me..It is urgent This is Hriday, working on windows authentication with Active Directory... My requirment is when a user sends a request to my web Applicatoin I want to...
0
by: RTT | last post by:
here is my current situation. I develop a program on my computer's localhost. From there i contact Active directory succesfull using a connectionstring like:...
2
by: Jim in Arizona | last post by:
My goal, somehow, is to populate a dropdownlist with all the user names in active directory. I don't even know where to begin, really. I added a reference to System.DirectoryServices so I could...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.