Solved

Values from Active Directory to Datatable in C#

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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 aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

732 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