troubleshooting Question

vb.net and ldap query results handling

Avatar of derek7467
derek7467 asked on
Databases.NET ProgrammingVisual Basic.NETActive DirectoryMicrosoft Server OS
4 Comments1 Solution339 ViewsLast Modified:
I wrote the below program to query a list of usernames.  What it does is this - it queries domain1 and if no match it queries domain2.  What i need help with is how to return my own defined values if the username isnt found in either domain.  Right now im handling it in a try-catch but thats not the right way:

Dim userIds As String() = TextBox1.Text.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
        For Each i As String In userIds
            Try
                Dim de As New DirectoryEntry("domain1.com/DC=domain1,DC=com")
                Dim LdapFilter As String = "(sAMAccountName=" & i & ")"
                Dim searcher As New DirectorySearcher(de, LdapFilter)
                Dim result As SearchResult = searcher.FindOne()
                Dim res As SearchResultCollection = searcher.FindAll()

                If res Is Nothing OrElse res.Count <= 0 Then
                    Dim tdbfg As New DirectoryEntry("LDAP://domain2.com/DC=domain2,DC=com")
                    Dim TDLdapFilter As String = "(sAMAccountName=" & i & ")"
                    Dim TDsearcher As New DirectorySearcher(tdbfg, TDLdapFilter)
                    Dim TDresult As SearchResult = TDsearcher.FindOne()
                    Dim item As ListViewItem = ListView1.Items.Add(i)
                    Dim ADEntry As DirectoryEntry = New DirectoryEntry(TDresult.Path)

                    If ADEntry.Properties("displayName").Value Is Nothing Then
                        item.SubItems.Add("Attribute not found")
                    Else
                        item.SubItems.Add(TDresult.Properties("displayName")(0).ToString())
                    End If

                    If ADEntry.Properties("title").Value Is Nothing Then
                        item.SubItems.Add("Attribute not found")
                    Else
                        item.SubItems.Add(TDresult.Properties("title")(0).ToString())
                    End If

                    If ADEntry.Properties("userPrincipalName").Value Is Nothing Then
                        item.SubItems.Add("Attribute not found")
                    Else
                        item.SubItems.Add(TDresult.Properties("userPrincipalName")(0).ToString())
                    End If

                ElseIf Not res.Count <= 0 Then
                    Dim ADEntry As DirectoryEntry = New DirectoryEntry(result.Path)
                    Dim item As ListViewItem = ListView1.Items.Add(i)

                    If ADEntry.Properties("manager").Value Is Nothing Then
                        item.SubItems.Add("Attribute not found")
                    Else
                        item.SubItems.Add(ADEntry.Properties("manager")(0).ToString())
                    End If

                    If ADEntry.Properties("title").Value Is Nothing Then
                        item.SubItems.Add("Attribute not found")
                    Else
                        item.SubItems.Add(ADEntry.Properties("title")(0).ToString())
                    End If

                    If ADEntry.Properties("userPrincipalName").Value Is Nothing Then
                        item.SubItems.Add("Attribute not found")
                    Else
                        item.SubItems.Add(ADEntry.Properties("userPrincipalName")(0).ToString())
                    End If
                End If
            Catch ex As Exception
                Dim item As ListViewItem = ListView1.Items.Add(i)
                item.SubItems.Add("Not found in US or CA Domain")
                item.SubItems.Add("Not found in US or CA Domain")
                item.SubItems.Add("Not found in US or CA Domain")
            End Try
        Next

Basically i want to remove the try-catch and actually check if the user is not found in either domain then return the below values to my listview.
ASKER CERTIFIED SOLUTION
Robert Schutt
Software Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros