Advertisement

02.21.2008 at 12:17PM PST, ID: 23182463
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.0

Problem with ignoring null or empty values returned from a LDAP query of AD using VB2005

Asked by WinkIT in Visual Studio, Active Directory

Tags: , ,

This is all being done in Visual Studio 2005 using Visual Basic.
This code works fine to input the search results into a datagrid.  However, when one of the fields is empty such as IPphone, mobile or any field for that mater, it bypass the following validator.

 If Not sruser.Properties("Mobile") Is Nothing

Since it passes the check, it then tries to execute:

Then Mobile = sruser.Properties("Mobile").Item(0)

Where Mobile is previously declared string.  When it tries to access that property, I recieve the given error.

I was wondering what type of check i need to create in order to catch these returns.  I've also tried a:

property.ToString = "" check and it fails as well.

Thanks in advance!Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
Dim deuser As DirectoryEntry
        Dim dsuser As DirectorySearcher
        Dim srusers As SearchResultCollection
 
 
        Try
 
            'create table to sort by lastname, firstname
            Dim tbSearchResults As New DataTable
            tbSearchResults.Columns.Add(New DataColumn("Employee"))
            tbSearchResults.Columns.Add(New DataColumn("Extension"))
            tbSearchResults.Columns.Add(New DataColumn("DirectDial"))
            tbSearchResults.Columns.Add(New DataColumn("Mobile"))
            tbSearchResults.Columns.Add(New DataColumn("Location"))
 
            'for AD search
            deuser = New DirectoryEntry("ldap://server.domain.com", "username", "password")
            dsuser = New DirectorySearcher("ldap://ADserver")
            dsuser.Filter = GetFilterString()
            dsuser.Sort.PropertyName = "sn"
            srusers = dsuser.FindAll
 
            If srusers Is Nothing Then
 
            Else
                'loop through users in search results
                For Each sruser As SearchResult In srusers
                    'assign names to variables
                    Dim fname As String = sruser.Properties("givenname").Item(0)
                    Dim lname As String = sruser.Properties("sn").Item(0)
                    Dim telephoneNumber As String = ""
                    If Not sruser.Properties("telephoneNumber") Is Nothing Then telephoneNumber = sruser.Properties("telephoneNumber").Item(0)
                    Dim IPphone As String = ""
                    If Not sruser.Properties("IPphone") Is Nothing Then IPphone = sruser.Properties("IPphone").Item(0)
                    Dim Mobile As String = ""
                    If Not sruser.Properties("Mobile") Is Nothing Then Mobile = sruser.Properties("Mobile").Item(0)
                    Dim Location As String = ""
                    If Not sruser.Properties("Physicaldeliveryofficename") Is Nothing Then Location = sruser.Properties("Physicaldeliveryofficename").Item(0)
                    Dim MidInitial As String = ""
                    If Not sruser.Properties("initials") Is Nothing Then MidInitial = CType(sruser.Properties("initials").Item(0), String).Substring(0, 1)
                    Dim AdsPath As String
                    AdsPath = sruser.Properties("AdsPath").Item(0)
 
 
 
                    'Exclude if in Termed Employees
                    If AdsPath.IndexOf("OU=Termed Employees") = -1 Then
                        'add a row to the Search Results with the AD info for the user
                        Dim drRow(4) As String
                        drRow(0) = lname + ", " + fname + " " + MidInitial
                        drRow(1) = telephoneNumber
                        drRow(2) = IPphone
                        drRow(3) = Mobile
                        drRow(4) = Location
 
                        tbSearchResults.Rows.Add(drRow)
 
 
                    End If
 
                Next
 
                'sort it
                Dim dvSearchResults As DataView = tbSearchResults.DefaultView
                dvSearchResults.Sort = "Employee"
 
                'only print if data is present
                If dvSearchResults.Count > 0 Then
                    DGrid.DataSource = tbSearchResults
                    DGrid.Refresh()
                End If
 
            End If
 
        Catch ex As Exception
            Dim sError As String = ex.Message.ToString
            Label1.Text = sError
        Finally
            'clean up
            If Not deuser Is Nothing Then deuser.Close()
            If Not deuser Is Nothing Then deuser.Dispose()
            If Not dsuser Is Nothing Then dsuser.Dispose()
            If Not srusers Is Nothing Then srusers.Dispose()
        End Try
    End Sub
[+][-]02.22.2008 at 02:18AM PST, ID: 20955965

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Visual Studio, Active Directory
Tags: Visual Basic, LDAP, IE, Firefox, Index was out of range. Must be non-negative and less than the size of the collection.
Sign Up Now!
Solution Provided By: Chris-Dent
Participating Experts: 1
Solution Grade: A
 
 
[+][-]02.22.2008 at 07:03AM PST, ID: 20957946

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628