We help IT Professionals succeed at work.

vb.net and ldap query results handling

325 Views
Last Modified: 2014-11-20
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

Open in new window


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.
Comment
Watch Question

Author

Commented:
no one can help?
Software Engineer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
thank you very much, i will try this as soon as i can and post back.

Author

Commented:
Appreciate you taking the time to do this.  It worked flawlessly, i really owe you.  Thanks for not just giving me an answer but helping me make a better question next time!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.