Dictionary Object

Discussion in 'ASP General' started by Johnny Klunk, Jul 8, 2003.

  1. Johnny Klunk

    Johnny Klunk Guest

    Wondering if someone can give me a hand with something that I'm sure is
    really easy - but damned if I know what I'm doing wrong. I'm trying to read
    the contents of a database into an ASP dictionary object. However I'm
    getting the error

    Microsoft VBScript runtime error '800a01c9'
    This key is already associated with an element of this collection
    /test.asp, line 21

    There's definately no repeated data in either column, it's currently only
    test data, one column is 1,2,3,4 etc the other is a,b,c,d. I can confirm
    it's reading the data properly if I use response.write instead of d.add
    The code I'm using is this:

    Set rsCount = Server.CreateObject ("ADODB.Recordset")
    sqlView = "select data,number from tblTemp"
    Set d=Server.CreateObject("Scripting.Dictionary")

    rsCount.Open sqlView, "dsn=dsn_hits_2"
    do while not rsCount.eof
    d.add rsCount("number"),rsCount("data")
    rsCount.MoveNext
    loop
    rsCount.close

    Set rsCount = Nothing




    Many thanks in advance.
     
    Johnny Klunk, Jul 8, 2003
    #1
    1. Advertising

  2. Johnny Klunk

    Ray at Guest

    When you add an item to the dictionary object, the key must be unique. The
    key is the first argument in the add method, which in this case, is
    rsCount("number"). What that error means is that it's hitting a record that
    has rsCount("number") with the same value as another record that has already
    been inserted into the dictionary object. i.e.


    number data
    1 Joe
    2 Kelly
    3 Frank
    2 Jorg

    When it gets to the Jorg record and tries to use 2 as the key, that'll throw
    an error since the Kelly item in the dictionary object already has a key of
    2.

    Possible Solutions:

    Use an array instead of a dictionary object.

    Use the primary key in your recordset if the key doesn't matter.

    Use a counter for the key and increase it by one in your loop if the key
    doesn't matter.

    Probably some other things.

    Ray at work


    "Johnny Klunk" <johnnyklunk@:rem0ve-this:johnnyklunk.com> wrote in message
    news:befbpm$47q$...
    > Wondering if someone can give me a hand with something that I'm sure is
    > really easy - but damned if I know what I'm doing wrong. I'm trying to

    read
    > the contents of a database into an ASP dictionary object. However I'm
    > getting the error
    >
    > Microsoft VBScript runtime error '800a01c9'
    > This key is already associated with an element of this collection
    > /test.asp, line 21
    >
    > There's definately no repeated data in either column, it's currently only
    > test data, one column is 1,2,3,4 etc the other is a,b,c,d. I can confirm
    > it's reading the data properly if I use response.write instead of d.add
    > The code I'm using is this:
    >
    > Set rsCount = Server.CreateObject ("ADODB.Recordset")
    > sqlView = "select data,number from tblTemp"
    > Set d=Server.CreateObject("Scripting.Dictionary")
    >
    > rsCount.Open sqlView, "dsn=dsn_hits_2"
    > do while not rsCount.eof
    > d.add rsCount("number"),rsCount("data")
    > rsCount.MoveNext
    > loop
    > rsCount.close
    >
    > Set rsCount = Nothing
    >
    >
    >
    >
    > Many thanks in advance.
    >
    >
     
    Ray at, Jul 8, 2003
    #2
    1. Advertising

  3. Johnny Klunk

    Johnny Klunk Guest

    "Ray at <%=sLocation%>" <> wrote in message
    news:...
    > When you add an item to the dictionary object, the key must be unique.

    The
    > key is the first argument in the add method, which in this case, is
    > rsCount("number"). What that error means is that it's hitting a record

    that
    > has rsCount("number") with the same value as another record that has

    already
    > been inserted into the dictionary object. i.e.



    Hi,
    Thanks for the response. The thing is, the data is definately not repeated.
    I entered the data manually to test and make there's no error. It's just
    1,2,3,4 etc..
    That's a good idea with the primary key. I've just tested again, setting
    each of my columns as the primary key to ensure the database has no repeated
    entries. I'm still getting the same thing. It's driving me nuts!
     
    Johnny Klunk, Jul 8, 2003
    #3
  4. Johnny Klunk

    Ray at Guest

    Try this then and look:

    rsCount.Open sqlView, "dsn=dsn_hits_2"
    do while not rsCount.eof
    response.write rsCount("number") & "," & rsCount("data") &
    "<br />"
    rsCount.MoveNext
    loop
    rsCount.close

    Ray at work


    "Johnny Klunk" <johnnyklunk@:rem0ve-this:johnnyklunk.com> wrote in message
    news:befd6c$rv5$...
    >
    > "Ray at <%=sLocation%>" <> wrote in message
    > news:...
    > > When you add an item to the dictionary object, the key must be unique.

    > The
    > > key is the first argument in the add method, which in this case, is
    > > rsCount("number"). What that error means is that it's hitting a record

    > that
    > > has rsCount("number") with the same value as another record that has

    > already
    > > been inserted into the dictionary object. i.e.

    >
    >
    > Hi,
    > Thanks for the response. The thing is, the data is definately not

    repeated.
    > I entered the data manually to test and make there's no error. It's just
    > 1,2,3,4 etc..
    > That's a good idea with the primary key. I've just tested again, setting
    > each of my columns as the primary key to ensure the database has no

    repeated
    > entries. I'm still getting the same thing. It's driving me nuts!
    >
    >
     
    Ray at, Jul 8, 2003
    #4
  5. Johnny Klunk

    Johnny Klunk Guest

    "Ray at <%=sLocation%>" <> wrote in message
    news:...
    > Try this then and look:
    >
    > rsCount.Open sqlView, "dsn=dsn_hits_2"
    > do while not rsCount.eof
    > response.write rsCount("number") & "," & rsCount("data") &
    > "<br />"
    > rsCount.MoveNext
    > loop
    > rsCount.close
    >
    > Ray at work
    >


    Ray,
    Thanks again for the quick response :) I've actually tried that idea - but
    to be sure, to be sure I pasted your code in to make sure the results
    matched. I've copy-pasted the output from test.asp below. You can see why
    this is driving me nuts ! As far as I see, every row has different data in
    each column.

    a,1
    b,2
    c,3
    d,4
    e,5
    f,6
    g,7
     
    Johnny Klunk, Jul 8, 2003
    #5
  6. "Johnny Klunk" <johnnyklunk@:rem0ve-this:johnnyklunk.com> wrote in
    message news:befbpm$47q$...

    >d.add rsCount("number"),rsCount("data")


    Try:
    d.add rsCount.Fields.Item("number").Value,
    rsCount.Fields.Item("data").Value

    Also you can take advantage of the fact that the dictionary object will
    create/overwrite a key based on whether it exists or not:
    d(rsCount.Fields.Item("number").Value) =
    rsCount.Fields.Item("data").Value

    However this method wreaks of "programming by side effect" so if the
    first method work, use that.

    HTH
    -Chris
     
    Chris Hohmann, Jul 8, 2003
    #6
  7. "Johnny Klunk" <johnnyklunk@:rem0ve-this:johnnyklunk.com> wrote in
    message news:befga2$cga$...
    >
    > > d.add rsCount.Fields.Item("number").Value,

    > rsCount.Fields.Item("data").Value
    > >

    >
    >
    > Yep, thats worked. Superb, thanks so much. No idea why my bit of

    code
    > didn't work, but I'm glad for a solution.
    >
    > Cheers
    >
    >

    You code was attempting to set the dictionary key to a field Object, not
    it's value. Whenever possible, try to explicitly reference values. When
    you reference the value of a field object implicitly (i.e.
    rsCount("number") you're asking the parser to do the following:

    1. Determine the default property/method for the rsCount object, the
    Fields collection
    2. Determine the default property/method for the Fields collection, the
    Item method
    3. Determine the default property/method for the Item object, the Value
    property
    4. Determine if the Value property is an object
    5. If it is an object do the whole object model default method/property
    traversal thing again.
    6. If not, return the Value

    All of these steps are also dependent on the context in which they are
    called. So sometimes the default thing happens, sometime it doesn't.
    There are also a lot more going on before, between and after each of
    these steps, but you get the idea. Always better to explicitly identify
    the "value" you want.

    HTH-
    Chris
     
    Chris Hohmann, Jul 8, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    483
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    362
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    3,331
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    573
    Chris Rebert
    Jul 29, 2010
  5. Fox

    dictionary within dictionary

    Fox, Mar 8, 2005, in forum: ASP General
    Replies:
    5
    Views:
    213
    Michael D. Kersey
    Mar 13, 2005
Loading...

Share This Page