locked
Getting type conversion error accessing property value from AD, using webservice call into autocomplete extender RRS feed

  • Question

  • User1916687561 posted

    Hello, I am using WebService to return data from active directory I am getting error at the following line:

    Dim phone As String = result.Properties("telephonenumber")(0).ToString()
    I am returning array to auto complete extender for textbox. The first few records go thorugh ok, but when it reaches a particular record , it does not like something in the phonenumber field.

      <WebMethod()> _
        Public Function findUser(ByVal prefixText As String) As String()
           
            Dim directory As DirectoryEntry = New DirectoryEntry ("LDAP://DC=ms,DC=XXXXXX,DC=com")
    
            Dim filter As String = "(&(cn=*" & prefixText & "*)(objectClass=Person)(objectClass=organizationalPerson)(objectClass=user)(!objectClass=computer)(!objectClass=nTFRSMember))"
    
            Dim strCats() As String = {"cn", "telephonenumber"}
            Dim items As New List(Of String)
            Dim dirUser As DirectorySearcher = New DirectorySearcher(directory, filter, strCats, SearchScope.Subtree)
          
            Dim results As SearchResultCollection = dirUser.FindAll
    
     
            Try
                Dim result As SearchResult
                Dim iCount As Integer
    
                If (results.Count > 0) Then
                    For Each result In results
                        For iCount = 0 To result.Properties("cn").Count - 1
                            
                            Dim fullName As String = result.Properties("cn")(0).ToString()
                            Dim phone As String = result.Properties("telephonenumber")(0).ToString() 
                            items.Add(fullName & ", " & phone)
                            iCount = iCount + 1
                        Next
                    Next
                End If
                items.Sort
                         
            Catch ex As Exception
                Print(ex.Message)
    
            End Try
            Return items.ToArray()
        End Function
    


     

     

    Error:

    System.InvalidCastException: Conversion from string &quot;Index was out of range. Must be &quot; to type 'Integer' is not valid. ---> System.FormatException: Input string was not in a correct format. at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) --- End of inner exception stack trace --- at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) at DirectoryServices.findUser(String prefixText) in C:\Documents and Settings\AtiqueM\My Documents\Visual Studio 2008\WebSites\EmployeeDirectory\App_Code\DirectoryServices.vb:line 73

    Tuesday, November 17, 2009 11:03 AM

All replies

  • User1916687561 posted

    When I step through the code I display the value for Dim phone As String = result.Properties("telephonenumber")(0).ToString()  

     in the watch window and it shows me this error.

    {"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index }

    I am guessing either it has nulls or spaces. I am not sure how to check for empty or null. I tried using DBNull but still errors out. Any other ideas?

    If Not IsDBNull(result.Properties("telephonenumber")(0)) Then

    phone = CStr(result.Properties("telephonenumber")(0))

    End If

     

    Tuesday, November 17, 2009 2:55 PM
  • User1916687561 posted

    I figured it out. Actually the property itself was missing for some records so obviously the value will not exist either. I guess you could say that there is bad data in our active directory and needs to be cleaned. Here is the final code to avoid the missing properties. 

    If

    (results.Count > 0) Then

    For Each result In results

    For Each prop As DictionaryEntry In result.Properties

    If prop.Key = "cn" Then

    fullName = result.Properties("cn")(0).ToString()

    End If

    If prop.Key = "telephonenumber" Then

    phone = result.Properties("telephonenumber")(0).ToString()

    End If

    If prop.Key = "othertelephone" Then

    ext = result.Properties("othertelephone")(0).ToString()

    End If

    Next

    items.Add(fullName & ", " & phone & ", " & ext)

     Next

     

    End If

     

    Monday, November 23, 2009 10:58 AM