LDAP query error

K

Kenneth H. Young

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(MouseEventArgs 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(Message& 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").Value)

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

Data3 = (result.GetDirectoryEntry().Properties("departmentNumber").Value)

Data4 = (result.GetDirectoryEntry().Properties("buildingName").Value)

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

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

Data7 = (result.GetDirectoryEntry().Properties("mail").Value)

Data8 = (result.GetDirectoryEntry().Properties("employeeType").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
 
P

Peter Huang [MSFT]

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("telephoneNumber").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("telephoneNumber").Value will
return a object array, so I think you may try assign the
result.GetDirectoryEntry().Properties("telephoneNumber").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.
 
K

Kenneth H. Young

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(KeyEventArgs kevent)
at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Message& 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(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
 
P

Peter Huang [MSFT]

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("telephoneNumber").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("telephoneNumber").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.
 
K

Kenneth H. Young

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("telephoneNumber").Value.GetType.ToString)

If TestVal1 = "System.Object[]" Then

Data6 = (result.GetDirectoryEntry().Properties("telephoneNumber").Value(0))

ElseIf TestVal1 = "System.String" Then

Data6 = (result.GetDirectoryEntry().Properties("telephoneNumber").Value)

Else

End If

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

Peter Huang [MSFT]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top