473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1860
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jeremy | last post by:
Hello. I have an asp.net application that resides on a non-DC / BDC Sharepoint Server (although it is logged into the domain). The application will perform lookups based on the current user...
4
by: m96 | last post by:
hi, i'm trying to make a query to a ldap server (version v2 or v3 doen't matter) with c#. the query works just fine but the problem is that i can't read the custom attributes/fields, since .net...
3
by: Jay | last post by:
I have a simple LDAP query (grabs all users from a particular AD group and populates a checkboxlist) that works perfectly fine on the development machine logged on locally as any user. When I...
1
by: Andrew | last post by:
Hey all, Working on revamping our Intranet here and making use of the LDPA, Active Directory, Directory Services, etc. that .Net provides. I am still fairly new on this subject, so the problem...
3
by: Chad Beckner | last post by:
I am starting to translate some code from ASP to ASP.NET (VB). I was able to query ADS to get a users groups that they belong to, and also query a group and get a list of users. However, I can't...
5
by: Bryan | last post by:
Hello, I have a asp.net app working with directory services on my Windows XP development machine. However when I moved the application over to our production server (Win 2000 Server) it no longer...
0
by: Kenneth H. Young | last post by:
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...
3
by: RJN | last post by:
Hi I've written a code that queries Windows LDAP server and works fine, but the same doesn't work when querying Solaris LDAP server. DirectoryEntry de = new...
7
by: MrHelpMe | last post by:
Sorry everyone, NOTE: I have posted this question to another site but unfortunately, am not getting the answers I need only because those helping haven't worked with ASP. I am in desperate...
3
by: eleland | last post by:
I am trying to bind to an LDAP server using php without success. I have a linux server running php compiled with open ldap. I am using the following code taken right from php.net: echo "<h3>LDAP...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.