469,934 Members | 2,534 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

LDAP query error

I am having difficulties with an LDAP client I'm writing. What I have
discovered causes the error is when the client queries the LDAP server and a
record contains two values entered in one field that is semicolon ";"
separated. i.e. A telephone number. In RED below.
How can I handle this?

Thanks is advance for any help.

Note: The LDAP server is not a Microsoft AD server.
Below is the
error.____________________________________________ __________________________________________________ __________

System.InvalidCastException: Cast from type 'Object()' to type 'String' is
not valid.
at Microsoft.VisualBasic.CompilerServices.StringType. FromObject(Object
Value)
at LDAPaddin.frmData.Button1_Click_1(Object sender, EventArgs e) in C:\VB
Projects\LDAPaddin\frmData.vb:line 337
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventAr gs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)

__________________________________________________ __________________________________________________ _____
Below is the pertinent code.
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim entry As New
DirectoryServices.DirectoryEntry("LDAP://server.example.com:389/ou=People,dc=ccs,dc=nrl,dc=navy,dc=mil")

Dim mySearcher As New System.DirectoryServices.DirectorySearcher(entry)

Dim result As System.DirectoryServices.SearchResult

Dim systemDirectory, systemService, Read1 As String

Dim Exists As Boolean

Dim Data1S, Data2S, Data3S, Data4S, Data5S, Data6S, Data7S As Object

systemDirectory = System.Environment.SystemDirectory

Dim MyFile, Data1, Data2, Data3, Data4, Data5, Data6, Data7, Data8 As String

Dim val1, val2, val3, val4, val5, val6, val7, val8 As String

Dim sField1, sField2, sField3, sField4, sField5, sField6, sField7, sField8
As String

val1 = "*" 'UID

val2 = "*" 'CN value Conanicle name

val3 = "*" 'Department #

val4 = "*" 'Building #

val5 = "*" 'Room #

val6 = "*" 'Telephone #

val7 = "*" 'Email address

val8 = "*" 'Employee Type

sField1 = "uid"

sField2 = "cn"

sField3 = "departmentNumber"

sField4 = "buildingName"

sField5 = "roomNumber"

sField6 = "telephoneNumber"

sField7 = "mail"

sField8 = "employeeType"

Me.DataSet11.Clear()

Me.OleDbDataAdapter1.Update(Me.DataSet11.Employees )

Me.PBar1.Visible = True

mySearcher.Filter = ("" & sField2 & "=" & val2 & "")

For Each result In mySearcher.FindAll

'On Error Resume Next

Data1 = (result.GetDirectoryEntry().Properties("uid").Valu e)

Data2 = (result.GetDirectoryEntry().Properties("cn").Value )

Data3 = (result.GetDirectoryEntry().Properties("department Number").Value)

Data4 = (result.GetDirectoryEntry().Properties("buildingNa me").Value)

Data5 = (result.GetDirectoryEntry().Properties("roomNumber ").Value)

Data6 = (result.GetDirectoryEntry().Properties("telephoneN umber").Value)
'LINE 337 This field has two phone numbers ; seperated. i.e. 555 123
4567;555 765 4321

Data7 = (result.GetDirectoryEntry().Properties("mail").Val ue)

Data8 = (result.GetDirectoryEntry().Properties("employeeTy pe").Value)

Me.DataSet11.Employees.AddEmployeesRow(Data1, Data2, Data3, Data4, Data5,
Data6, Data7, Data8)

PBar1.PerformStep()

Next

PBar1.Value = 0

Me.DataSet11.AcceptChanges()

MsgBox("Query complete!", MsgBoxStyle.OKOnly, "")

Me.PBar1.Visible = False

End Sub
Nov 21 '05 #1
5 1727
Hi

Based on my understanding the exception will occur when we call the line
below, if I have any misunderstanding, please feel free to post here.

Data6 = (result.GetDirectoryEntry().Properties("telephoneN umber").Value)
'LINE 337 This field has two phone numbers ; seperated. i.e. 555 123
4567;555 765 4321

From the MSDN the Properties("xxxx").Value should be object type.
Also from the callstack,
System.InvalidCastException: Cast from type 'Object()' to type 'String' is
not valid.

The result.GetDirectoryEntry().Properties("telephoneNu mber").Value will
return a object array, so I think you may try assign the
result.GetDirectoryEntry().Properties("telephoneNu mber").Value to an object
array to see if that works or an object to see if that works.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #2
Thanks for the reply. This is driving me insane! If I Dim DataX as
Object and run the query it returns every field excluding ones with two
entries. For entries with two telephoneNumber numbers that are ";"
separated I get no return. If I rem "'On error Resume Next" I get the
following exception on line 424.

Line 424:
Me.DataSet11.Employees.AddEmployeesRow(Data1, Data2, Data3, Data4, Data5,
Data6, Data7, Data8)
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.InvalidCastException: Cast from type 'Object()' to type 'String' is
not valid.
at Microsoft.VisualBasic.CompilerServices.StringType. FromObject(Object
Value)
at LDAPaddin.frmData.Button1_Click_1(Object sender, EventArgs e) in C:\VB
Projects\LDAPaddin\frmData.vb:line 424
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.ButtonBase.OnKeyUp(KeyEventAr gs kevent)
at System.Windows.Forms.Control.ProcessKeyEventArgs(M essage& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Mes sage& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
""Peter Huang" [MSFT]" <v-******@online.microsoft.com> wrote in message
news:TI**************@TK2MSFTNGXA02.phx.gbl...
Hi

Based on my understanding the exception will occur when we call the line
below, if I have any misunderstanding, please feel free to post here.

Data6 = (result.GetDirectoryEntry().Properties("telephoneN umber").Value)
'LINE 337 This field has two phone numbers ; seperated. i.e. 555 123
4567;555 765 4321

From the MSDN the Properties("xxxx").Value should be object type.
Also from the callstack,
System.InvalidCastException: Cast from type 'Object()' to type 'String' is
not valid.

The result.GetDirectoryEntry().Properties("telephoneNu mber").Value will
return a object array, so I think you may try assign the
result.GetDirectoryEntry().Properties("telephoneNu mber").Value to an
object
array to see if that works or an object to see if that works.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no
rights.

Nov 21 '05 #3
Hi

From the call stack I assume one of the parameters is of type Object array.
Here I assume it is Data6 because of
Data6 = (result.GetDirectoryEntry().Properties("telephoneN umber").Value)
'LINE 337 This field has two phone numbers ; seperated. i.e. 555 123
4567;555 765 4321

So I think you may try to parse the data6 using the array syntax.
dim data6() as object
data6= (result.GetDirectoryEntry().Properties("telephoneN umber").Value)
For Each o As Object In data6
Console.WriteLine(o.ToString())
Next

You may have a try and let me know the result.
If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #4
OK, that didn't work either but it put me on the right track.
This is what has finally worked: Don't know if it's the best way but it
works!

I'd also like to extend my gratitude to you for all your help.
Thanks for helping out a novice. :-)

Dim TestVal1, Data6 as String
TestVal1 =
(result.GetDirectoryEntry().Properties("telephoneN umber").Value.GetType.ToString)

If TestVal1 = "System.Object[]" Then

Data6 = (result.GetDirectoryEntry().Properties("telephoneN umber").Value(0))

ElseIf TestVal1 = "System.String" Then

Data6 = (result.GetDirectoryEntry().Properties("telephoneN umber").Value)

Else

End If

Note: This pulls back only the first number which is all I am intersted in.

""Peter Huang" [MSFT]" <v-******@online.microsoft.com> wrote in message
news:Dx**************@TK2MSFTNGXA02.phx.gbl...
Hi

From the call stack I assume one of the parameters is of type Object
array.
Here I assume it is Data6 because of
Data6 = (result.GetDirectoryEntry().Properties("telephoneN umber").Value)
'LINE 337 This field has two phone numbers ; seperated. i.e. 555 123
4567;555 765 4321

So I think you may try to parse the data6 using the array syntax.
dim data6() as object
data6= (result.GetDirectoryEntry().Properties("telephoneN umber").Value)
For Each o As Object In data6
Console.WriteLine(o.ToString())
Next

You may have a try and let me know the result.
If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no
rights.

Nov 21 '05 #5
Hi

I am glad that works for you.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Andrew | last post: by
5 posts views Thread by Bryan | last post: by
reply views Thread by Kenneth H. Young | last post: by
3 posts views Thread by RJN | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.