Solved

Values from Active Directory to Datatable in C#

Posted on 2006-07-04
3
989 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now