asp classic - Inserting a disconnected recordset into a dictionary object? -
just wondering if has seen issue, when trying insert items disconnected recordset classic asp dictionary object error "this key associated element of collection".
this not case, key elements unique, yes triple checked data :)
if shove data local variable works fine (see code below). while i'm okay has else seen issue or know why doesn't work?
set options=server.createobject("scripting.dictionary") function populate(id) ' works dim strsql, rs, name, id if options.count = 0 strsql = "select name, id options unique = " & safesql(id) set rs = runsqlreturnrs(strsql, null, "populate") ' returns disconnected recordset while not rs.eof name = rs("name") id = rs("id") options.add name, id rs.movenext loop rs.close end if end function function populate2(id) ' not work dim strsql, rs if options.count = 0 strsql = "select name, id options unique = " & safesql(id) set rs = runsqlreturnrs(strsql, null, "populate") ' returns disconnected recordset while not rs.eof options.add rs("name"), rs("id") rs.movenext loop rs.close end if end function
by request of user shadow wizard, disconnected record function, (note my_conn database connection , open time function called)
function runsqlreturnrs(sqlstmt, params(), fromfunction) ''//create ado objects dim rs, cmd set rs = server.createobject("adodb.recordset") if not zerolength(sqlstmt) set cmd = server.createobject("adodb.command") ''//init ado objects & stored proc parameters cmd.activeconnection = my_conn cmd.commandtext = sqlstmt cmd.commandtype = adcmdtext cmd.commandtimeout = 900 ''// propietary function put params in cmd collectparams cmd, params ''//execute query readonly rs.cursorlocation = aduseclient on error resume next rs.open cmd, , adopenforwardonly, adlockreadonly if err.number <> 0 response.write "error: "&err.description & "<br>fromfunction=" & fromfunction & "<br>" : response.end on error goto 0 ''// disconnect recordset set cmd.activeconnection = nothing set cmd = nothing set rs.activeconnection = nothing end if ''// return resultant recordset set runsqlreturnrs = rs end function
ok, tricky figured happened.
the add()
method of vbscript dictionary object accepting object both key , value arguments. object can of type: number, string, date, complex type etc.
something not commonly known when write rs("name")
"rs" of type recordset, not return primitive type rather returns field object.
however, if try assign local variable, vbscript smart enough default property of object , if present, execute it. field object have such property, returns value of field.
when have this:
name = rs("name")
it means:
name = rs("name").value
so in first method, giving string key, , good. in second method, pass field object itself, , vbscript lack proper comparison mechanism between field objects thinks they're same.
to have second method work without using local variables, change this:
do while not rs.eof options.add rs("name").value, rs("id").value rs.movenext loop
Comments
Post a Comment