articles:

classic asp dictionary not working

Found an interesting feature of the classic dictionary object. Consider this code block:

dim conn
dim rs
dim myDictionary

set conn = server.CreateObject("ADODB.connection")
conn.open connPublic
'connPublic is just a normal connection string

set rs = server.CreateObject("ADODB.recordset")

set myDictionary = Server.CreateObject("Scripting.Dictionary")

rs.open "SELECT [id],[pagename] FROM [maincontent]",conn,3

do while not rs.eof
    myDictionary.add cstr(rs("id")), rs("pagename")
    rs.movenext
loop

rs.close
set rs = nothing
conn.close
set conn = nothing
dim i
for each i in myDictionary
    response.write(myDictionary.item(i) & "<br>")
next

set myDictionary = nothing

This seemingly plausible block of code returns the highly descriptive error '80020009'.

It appears that this error is due to the dictionary add method not really doing what it says on the tin, i.e. not adding. Fortunately there is a work around for this problem - change

myDictionary.add cstr(rs("id")), rs("pagename")

for

myDictionary(cstr(rs("id"))) = rs("pagename")

Doing this forces the dictionary object to create a new item with the name you have specified, much like what you would expect the 'add' method to do...

Post a comment

name:
(optional)

email:
(optional)

comment:

call us on 01344 762988