Link to home
Start Free TrialLog in
Avatar of williamwatkins
williamwatkins

asked on

Novell eDirectory LDAP lookup using Visual Studio .Net 2003 VB.NET - InvalidCastException

What I am doing is using an LDAP lookup to query an individual group in eDirectory, then I'm setting their personal information as parameters for a stored procedure which then adds their information into a SQL Database.  Everything works fine except for the LDAP photo property.  So no need to worry about the LDAP piece.  I'm getting a Cast from type 'Byte()' to type 'String' is not valid on the line I declare photo.  As far as I can tell the LDAP is returning a Byte() which cannot be casted to a string.  Is there a way around this.  I just need to assign the result to a stored procedure parameter.  My code is below and is part of the Page_Load event.  A value for photo when the exception is being thrown is like this...  /myFolder/Photos/OurPhotos/A_Fake_Folder/cn=XXX_ou=XXX_o=XX.jpg


            Dim oDirEntry As System.DirectoryServices.DirectoryEntry
            Dim sUserPath As String

            oDirEntry = New System.DirectoryServices.DirectoryEntry("LDAP://XXX.XXX.X.XXX/cn=XXX,ou=XXX,o=XXX")

            SqlConnection1.Open()
            For Each sUserPath In oDirEntry.Properties("member")
                Dim oUserEntry As New System.DirectoryServices.DirectoryEntry("LDAP://XXX.XXX.X.XXX/" & sUserPath)
                Dim firstName As String = CType(oUserEntry.Properties("givenName").Value, String)
                Dim lastName As String = CType(oUserEntry.Properties("sn").Value, String)
                Dim photo As String = CType(oUserEntry.Properties("photo").Value, String)     <<<==== The problem is here

                If firstName > "" And email > "" Then
                    SqlCommand1.Parameters("@FirstName").Value = firstName
                    SqlCommand1.Parameters("@LastName").Value = lastName
                    SqlCommand1.Parameters("@Photo").Value = photo

                    SqlCommand1.ExecuteNonQuery()
                End If
            Next
            SqlConnection1.Close()
        End If
Avatar of williamwatkins
williamwatkins

ASKER

Anyone have an idea???  If you need more information please let me know!
ASKER CERTIFIED SOLUTION
Avatar of ihenry
ihenry

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
Yes, that is exactly what I was looking for.  I have one small problem...  Some of the values in eDirectory are null.  So when I call on the fuction I'm getting an "Array cannot be null. Parameter name: bytes" error.  I modified the fuction to not convert if the value passed in is null.  I'm still getting the same error though.  I'm missing something???  My code is below...

Thanks ihenry for the help so far.  You knew exactly what I needed.


    Public Function FromASCIIByteArray(ByVal characters As Byte()) As String
        Dim encoding As New System.Text.ASCIIEncoding
        If Not IsDBNull(characters) Then
            Dim constructedString As String = encoding.GetString(characters)
            Return constructedString
        Else
            Return ""
        End If
    End Function
Use PropertyCollection.Contains method to check whether the DirectoryEntry object has the photo attribute.

Dim photo As String
If oUserEntry.Properties.Contains( "photo" ) Then
   photo = FromASCIIByteArray( oUserEntry.Properties("photo") )
End If

Or modify the FromASCIIByteArray function to

Public Function FromASCIIByteArray(ByVal characters As Byte()) As String
     If characters Is Nothing Then
        Return ""
     End If
     Dim encoding As New System.Text.ASCIIEncoding
     .....
     .....
     ' continue here
End Function
Perfect!  I used the PropertyCollection.Contains method, tweaked it a little bit, and got the results I wanted.  Thanks again ihenry and great job!
You're welcome. Please post .NET related question to .NET TA, you'd get better chances there.