Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HELP: Cannot retrieve values from ADO Recordset returned by LDAP query!

1 view
Skip to first unread message

ranjit

unread,
Dec 20, 2002, 2:24:29 PM12/20/02
to
Good Day - I come with humility to seek your benevolent wisdom.

I am an MS newbie (old Unix-head) and have managed to cobble together
the following ASP (VBScript) to retrieve records from an LDAP
directory. Based on the record count, I know that my code is
retrieving the data, but I am unable to get the field values. I have
added some debugging statements that show the field data types as
DBTYPE_VARIANT, but every attempt to coax the field values out has met
with abject failure (Type Mismatch).

You should be able to cut-n-paste the following code directly into
your ASP and check it out, if you are so inclined. As shown below, the
Response.Write statement that does not work is commented out. Remove
the comment quote to see the failure.

Many thanks,
Ranjit

=== 8< === 8< === 8< === 8< === 8< === 8< === 8< === 8< ===
<%@ Language=VBScript %>

<html>
<head>
<title>Testing LDAP data retrieval</title>
</head>
<body>
<font face="MS Gothic" size=+2>

<%
Dim oConnection
Dim oRecordset
Dim oCommand
Dim oArray

Set oConnection = CreateObject("ADODB.Connection")
Set oRecordset = CreateObject("ADODB.Recordset")
Set oCommand = CreateObject("ADODB.Command")

oConnection.Provider = "ADsDSOObject"
oConnection.Properties("User ID") = ""
oConnection.Properties("Password") = ""
oConnection.Properties("Encrypt Password") = False
oConnection.Open "ADs Provider"

' The query can be tested in your browser by visiting
' http://www.emailman.com/ldap/public.html, and then
' clicking on the LDAP link of University of Alaska
' Southeast Campus (3rd row). This brings up a form into
' which you can fill in the name 'jones' and ask for the
' entire record to be returned

strQuery = "<LDAP://ldap.uas.alaska.edu>;(SN=JONES);CN,Title,GivenName,Mail;subtree"

oCommand.ActiveConnection = oConnection
oCommand.CommandText = strQuery
oCommand.Properties("Page Size") = 99
Set oRecordset = oCommand.Execute

' The record count is written correctly
Response.Write("data retrieved;" & oRecordset.RecordCount & " rows
returned<BR>")

' Enumerate Fields collection of Employees table
' All fields are of type DBTYPE_VARIANT (12) ref: ADO docs
' My guess is that this is causing the problem, but its only a guess

For Each fld In oRecordset.Fields
' Translate field-type code to text
FieldType = Cstr(fld.Type)
' Write out the field type
Response.Write "Field: " & fld.Name & "; Type: " & FieldType &
"<BR>"
Next

' Here is where I have trouble - I am unable to retrieve (and print)
the
' values of the fields in the Recordset. I have tried Cstr, copying
data
' into an array, the GetRow method, but all of these fail with
errors
' about data type mismatches - HELP!

While Not oRecordSet.EOF
' When the following line is commented out, the page works
' Response.Write Cstr(oRecordSet.Fields("CN")) & "<BR>"
oRecordSet.MoveNext
Wend

oRecordSet.Close
oConnection.Close

Set oRecordset = Nothing
Set oCommand = Nothing
Set oConnection = Nothing

Response.Write("OK")

%>

</font>
</body>
</html>

Torgeir Bakken (MVP)

unread,
Dec 20, 2002, 7:22:46 PM12/20/02
to
ranjit wrote:

> Good Day - I come with humility to seek your benevolent wisdom.
>
> I am an MS newbie (old Unix-head) and have managed to cobble together
> the following ASP (VBScript) to retrieve records from an LDAP
> directory. Based on the record count, I know that my code is
> retrieving the data, but I am unable to get the field values. I have
> added some debugging statements that show the field data types as
> DBTYPE_VARIANT, but every attempt to coax the field values out has met
> with abject failure (Type Mismatch).

Hi

This is how I iterate a recordset with a WSH script (you need to substitute Wscript.Echo
with Response.Write):

' Iterate through the results
If oRecordset.Eof and oRecordSet.Bof Then
WScript.Echo "No records were found"
Else
oRecordSet.MoveFirst
Do Until oRecordSet.EOF
For i = 0 To oRecordSet.Fields.Count - 1

aField = oRecordSet.Fields(i)

'For Multi Value attribute
If IsArray(aField) Then
For Each sItem In aField
Wscript.Echo oRecordSet.Fields(i).Name & " = " & sItem
Next

' alternatively
'For j = 0 To Ubound(aField)
' Wscript.Echo oRecordSet.Fields(i).Name _
' & " = " & aField(j)
'Next

Else
'For Single Value attribute
Wscript.Echo oRecordSet.Fields(i).Name _
& " = " & oRecordSet.Fields(i).Value
End If
Next
oRecordSet.MoveNext
Loop
End If


--
torgeir
Microsoft MVP Scripting and WMI
Porsgrunn Norway


ranjit

unread,
Dec 23, 2002, 9:14:31 PM12/23/02
to
Thanks very much! that worked - i had to convert the script into VBS
and use the debugger to discover that each item in the recordset was
actually an array - your code took care of the rest.

Many thanks again,
Ranjit

"Torgeir Bakken (MVP)" <Torgeir.B...@hydro.com> wrote in message news:<3E03B455...@hydro.com>...

0 new messages