Itβs just interesting if anyone saw this problem while trying to insert elements from a disconnected recordset into a classic ASP dictionary object. I get the error "This key is already associated with an item in this collection."
This is not the case, all the key elements are unique, yes, I checked the data three times :)
If I write the data to a local variable, it works fine (see code below). Although I'm fine with this, has anyone else seen this problem or knew why it is not working?
Set Options=Server.CreateObject("Scripting.Dictionary")
function Populate(ID) ' WORKS
Dim strsql, rs, name, id
if Options.Count = 0 then
strsql = "select name, ID from options where unique = " & SafeSQL(ID)
Set rs = RunSQLReturnRS(strsql, null, "Populate") ' returns a disconnected recordset
do 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) ' DOES NOT WORK
Dim strsql, rs
if Options.Count = 0 then
strsql = "select name, ID from options where unique = " & SafeSQL(ID)
Set rs = RunSQLReturnRS(strsql, null, "Populate") ' returns a disconnected recordset
do while not rs.eof
Options.Add rs("name"), rs("id")
rs.movenext
loop
rs.close
end if
end function
At the request of the Shadow Wizard user, the disabled recording function (note that my_conn is a database connection and is already open by the time this function is called)
Function RunSQLReturnRS(sqlstmt, params(), fromfunction)
''//Create the ADO objects
Dim rs, cmd
Set rs = server.createobject("ADODB.Recordset")
If Not zerolength(sqlstmt) then
Set cmd = server.createobject("ADODB.Command")
''//Init the ADO objects & the stored proc parameters
cmd.ActiveConnection = my_conn
cmd.CommandText = sqlstmt
cmd.CommandType = adCmdText
cmd.CommandTimeout = 900
''// propietary function that put params in the cmd
collectParams cmd, params
''//Execute the query for readonly
rs.CursorLocation = adUseClient
On Error Resume Next
rs.Open cmd, , adOpenForwardOnly, adLockReadOnly
If Err.Number <> 0 Then Response.write "Error: "&Err.Description & "<br>fromfunction=" & fromfunction & "<br>" : Response.end
On Error Goto 0
''// Disconnect the recordset
Set cmd.ActiveConnection = Nothing
Set cmd = Nothing
Set rs.ActiveConnection = Nothing
End if
''// Return the resultant recordset
Set RunSQLReturnRS = rs
End Function