[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1004
  • Last Modified:

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
0
williamwatkins
Asked:
williamwatkins
  • 3
  • 3
1 Solution
 
williamwatkinsAuthor Commented:
Anyone have an idea???  If you need more information please let me know!
0
 
ihenryCommented:
photo attribute returns octet stream or byte array data which you can't directly convert it to string. Use one of these two functions to convert from byte array back to string.

Public Function FromASCIIByteArray( characters As Byte() ) As String
      Dim encoding As New ASCIIEncoding()
      Dim constructedString As String = encoding.GetString( characters )
      Return constructedString
End Function

Public Function FromUnicodeByteArray( characters As Byte() ) As String
      Dim encoding As New UnicodeEncoding()
      Dim constructedString As String = encoding.GetString( characters )
      Return constructedString
End Function

0
 
williamwatkinsAuthor Commented:
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ihenryCommented:
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
0
 
williamwatkinsAuthor Commented:
Perfect!  I used the PropertyCollection.Contains method, tweaked it a little bit, and got the results I wanted.  Thanks again ihenry and great job!
0
 
ihenryCommented:
You're welcome. Please post .NET related question to .NET TA, you'd get better chances there.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now