www.webdeveloper.com
Results 1 to 4 of 4

Thread: Strange Dictionary object Error.

Hybrid View

  1. #1
    Join Date
    Feb 2005
    Posts
    4

    Strange Dictionary object Error.

    Need some help while my hair is still (mostly) intact. I've isolated this code out of a larger file (which i'm developing at the moment).

    Code is as follows:



    <!--#include file="dbconfile.asp"-->
    <%
    set Rs=server.createobject("adodb.recordset")
    set cratedict=server.createobject("scripting.dictionary")
    Rs.open "select top 5 bindtype, rate from current_rates where type='sp' and identifier=2051 order by bindtype",DatabaseConnection
    while not Rs.eof
    response.write replace(trim(Rs("bindtype")),"_","")&" - "&trim(Rs("rate"))&"<br>"
    cratedict.add replace(trim(Rs("bindtype")),"_",""),Rs("rate")

    Rs.movenext
    wend
    Rs.close
    response.write "<hr>"
    response.write cratedict.count&" count<br>"
    g=cratedict.item("bp")
    %>


    /end code

    Result is as follows:

    bp - 1
    bterms - 1
    billtoperson - 2161
    cdpd - 2
    cdps - 0.1

    ------------------
    5 count

    ADODB.Field error '800a0d5c'

    Object is no longer valid.

    testdict.asp, line 15


    /end result

    Why would it give me the count on line 14, but not return the value on line 15?
    Now, the thing is, i'm using dictonary objects in other scripts with no errors, and i can't for the life of me figure out what's going on.

  2. #2
    Join Date
    Feb 2005
    Posts
    4
    no further responses needed, it turns out that my rate value needed to be trimmed. all is good, except for the state of my hair, but i can probably find most of it and paste it back on.

  3. #3
    Join Date
    Sep 2005
    Posts
    1

    explanation

    I also ran into this, and spent way too long trying to figure it out.

    For future visitors to this thread:

    It turns out that the two parameters passed to the scripting.dictionary "add" function are BYREFerence and not BYVALue, so it doesn't make a copy of the adodb.recordset data, but rather just puts a pointer to the adodb.recordset data. Of course, then you get finished with the rs (or move to the next record!) the dictionary.item() pointer goes to the wrong place, and then the adodb error.

    So run your parameters through any function (trim, int) or assign them to a variable ( a=rs(0) : b=rs(0) : odict.add a,b ) and you won't get the weird errors, or pull out any hair!


    Jeremy

  4. #4
    Join Date
    Jan 2013
    Posts
    1

    alternative solution

    This has caught me out a couple of times before. My preferred solution is to use the Value property:

    Code:
    cratedict.add replace(trim(Rs("bindtype").Value),"_",""),Rs("rate").Value
    Perhaps a tad more elegant than Trim(), using a variable, etc.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles