Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error while retreiving particular user details from AD

Status
Not open for further replies.

pusalaanita

IS-IT--Management
Feb 22, 2006
12
NO
am geting the following exception

: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 196: internal static string GetProperty(DirectoryEntry oDE, string PropertyName)
Line 197: {
Line 198: if (oDE.Properties.Contains(PropertyName))
Line 199: {
Line 200: return oDE.Properties[PropertyName][0].ToString(); I am using this to retrieve details all users(LoadAllUsers) its works fine but when I am and retrieving a particular user details (LoadUser(UserName))it is giving the above exception.I am not able to trace out the error.I have checked with many other code samples and they r using the same.Please , help me in solving it.TksBye

 
Try stepping through the code...you'll see that a particular object hasn't been set top a specific instance (for example, it may have to be declared as New).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
What is UserName: it probably has to be the sAMAccountName.
I did some AD querying using c#, some parts were a challenge.
If you could expand the code snippet, maybe it will ring a bell.
-raghu
 
If it is with only one user ot any single user ?
do you do a findOne(), if findAll() works, findOne should also work.
how is your query: in c# I have something like this for findOne():

string strFilter = "(samaccountname=" + uid +")";

// Connect to Domain controller: LDAP string
System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry(_strLDAP);


// Search object
System.DirectoryServices.DirectorySearcher mySearcher =new System.DirectoryServices.DirectorySearcher(entry);

// Search filters
mySearcher.Filter = strFilter;
mySearcher.SearchScope=SearchScope.Subtree;


// Get the directory entry
System.DirectoryServices.SearchResult result=mySearcher.FindOne();
if (result != null)
{
System.DirectoryServices.DirectoryEntry de=result.GetDirectoryEntry();

// Do process ...
 
Here is a function snippet. This gets all the attributes for an account:

// Connect to ldap
System.DirectoryServices.DirectoryEntry entry =new System.DirectoryServices.DirectoryEntry(_strLDAP); //your ldap


// Search object
System.DirectoryServices.DirectorySearcher mySearcher =new System.DirectoryServices.DirectorySearcher(entry);


// Search filters
mySearcher.Filter = filter; // filter=filter="(&(&(objectClass=user)(SAMAccountName="+ uid.Trim() +")))"; // uid=your user Id


mySearcher.SearchScope=SearchScope.Subtree;
// FindOne() Vs FindAll() ...
SearchResult mySearchResult = mySearcher.FindOne();


if (mySearchResult != null )
{
// Print the properties.
ResultPropertyCollection myResult;
myResult=mySearchResult.Properties;
System.DirectoryServices.DirectoryEntry de = mySearchResult.GetDirectoryEntry();

foreach(string myKey in myResult.PropertyNames)
{

foreach( Object myCollection in myResult[myKey])
{
attribValue=myCollection.ToString();
Console.WriteLine(myKey + ":" + attribValue);
}

}
}


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top