?
Solved

Values from Active Directory to Datatable in C#

Posted on 2006-07-04
3
Medium Priority
?
1,124 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 2000 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

Technology Partners: 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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

801 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