Solved

Values from Active Directory to Datatable in C#

Posted on 2006-07-04
3
1,087 Views
Last Modified: 2008-03-06
Hi!
I'm having some trouble with my code to get data from Active Directory. The code works fine for non optional fields in my AD, but when I try to add a row where one or more columnvalues are blank, the entire row is left out of the DataTable and I cant view it in my DataGridView . Is there anyway to allow blank columnvalues in a DataRow?
Heres my code:

public DataTable doQuery()
        {
           
             System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry("LDAP://mydomain.com");
            System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
            mySearcher.Filter =("(objectClass=user)");
           
            DataTable adTabell = new DataTable("Brukertabell");
            DataColumn adKolonne = adTabell.Columns.Add("Fornavn", typeof(string));
            adKolonne.AllowDBNull = false;
            adKolonne.Unique = true;
            adTabell.Columns.Add("Etternavn", typeof(string));
            adTabell.Columns.Add("Tittel", typeof(string));
            adTabell.Columns.Add("Selskap", typeof(string));
            adTabell.Columns.Add("Sted", typeof(string));
            adTabell.Columns.Add("Mobil", typeof(string));
           
            foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
            {
             
                try
                {
                    System.DirectoryServices.DirectoryEntry de = resEnt.GetDirectoryEntry();
               
                    DataRow adRad = adTabell.NewRow();
                    adRad["Fornavn"] = de.Properties["DisplayName"].Value.ToString();
                    adRad["Etternavn"] = de.Properties["GivenName"].Value.ToString();
                    adRad["Tittel"] = de.Properties["Title"].Value.ToString();
                    adRad["Selskap"] = de.Properties["Company"].Value.ToString();
                    adRad["Sted"] = de.Properties["Department"].Value.ToString();
                    adRad["Mobil"] = de.Properties["Mobile"].Value.ToString();
                    adTabell.Rows.Add(adRad);

                }
                catch (Exception e)
                {
                }
               

            }
            System.Windows.Forms.MessageBox.Show("rowCount: " + adTabell.Rows.Count);
                        return adTabell;

        }
    }
0
Comment
Question by:frodehoe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 12

Expert Comment

by:sumix
ID: 17040232
The DataRow is not the problem.
 If any of the property of the DirectoryEntry instance has a null value you get an exception when you try to convert it to a string, then catch block is entered and the row is not added to the table. You should test each value before setting the field, something like

    if (de.Properties["GivenName"].Value != null)
          adRad["Etternavn"] = de.Properties["GivenName"].Value.ToString();
0
 
LVL 12

Accepted Solution

by:
sumix earned 500 total points
ID: 17040255
Or, simpler, just remove the ToString() conversions:
   adRad["Fornavn"] = de.Properties["DisplayName"].Value;
0
 

Author Comment

by:frodehoe
ID: 17040672
Thanks sumix. Removed the .tostring() and it works like a charm :)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question