Solved

Values from Active Directory to Datatable in C#

Posted on 2006-07-04
3
1,059 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
  • 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

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

679 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