Link to home
Start Free TrialLog in
Avatar of derek7467
derek7467

asked on

vb.net and ldap query results handling

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.
Avatar of derek7467
derek7467

ASKER

no one can help?
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you very much, i will try this as soon as i can and post back.
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!