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_Fo lder/cn=XX X_ou=XXX_o =XX.jpg
Dim oDirEntry As System.DirectoryServices.D irectoryEn try
Dim sUserPath As String
oDirEntry = New System.DirectoryServices.D irectoryEn try("LDAP: //XXX.XXX. X.XXX/cn=X XX,ou=XXX, o=XXX")
SqlConnection1.Open()
For Each sUserPath In oDirEntry.Properties("memb er")
Dim oUserEntry As New System.DirectoryServices.D irectoryEn try("LDAP: //XXX.XXX. X.XXX/" & sUserPath)
Dim firstName As String = CType(oUserEntry.Propertie s("givenNa me").Value , String)
Dim lastName As String = CType(oUserEntry.Propertie s("sn").Va lue, String)
Dim photo As String = CType(oUserEntry.Propertie s("photo") .Value, String) <<<==== The problem is here
If firstName > "" And email > "" Then
SqlCommand1.Parameters("@F irstName") .Value = firstName
SqlCommand1.Parameters("@L astName"). Value = lastName
SqlCommand1.Parameters("@P hoto").Val ue = photo
SqlCommand1.ExecuteNonQuer y()
End If
Next
SqlConnection1.Close()
End If
Dim oDirEntry As System.DirectoryServices.D
Dim sUserPath As String
oDirEntry = New System.DirectoryServices.D
SqlConnection1.Open()
For Each sUserPath In oDirEntry.Properties("memb
Dim oUserEntry As New System.DirectoryServices.D
Dim firstName As String = CType(oUserEntry.Propertie
Dim lastName As String = CType(oUserEntry.Propertie
Dim photo As String = CType(oUserEntry.Propertie
If firstName > "" And email > "" Then
SqlCommand1.Parameters("@F
SqlCommand1.Parameters("@L
SqlCommand1.Parameters("@P
SqlCommand1.ExecuteNonQuer
End If
Next
SqlConnection1.Close()
End If
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(charact ers)
Return constructedString
Else
Return ""
End If
End Function
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(charact
Return constructedString
Else
Return ""
End If
End Function
Use PropertyCollection.Contain s method to check whether the DirectoryEntry object has the photo attribute.
Dim photo As String
If oUserEntry.Properties.Cont ains( "photo" ) Then
photo = FromASCIIByteArray( oUserEntry.Properties("pho to") )
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
Dim photo As String
If oUserEntry.Properties.Cont
photo = FromASCIIByteArray( oUserEntry.Properties("pho
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
ASKER
Perfect! I used the PropertyCollection.Contain s 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.
ASKER