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.
.NET ProgrammingVisual Basic.NETActive DirectoryMicrosoft Server OSDatabases

Avatar of undefined
Last Comment
derek7467

8/22/2022 - Mon
derek7467

ASKER
no one can help?
ASKER CERTIFIED SOLUTION
Robert Schutt

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
derek7467

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

ASKER
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!
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck