Ask a Question related to ASP, Design and Development.

  1. #1

    Default Dictionary Object

    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 Guest

  2. Similar Questions and Discussions

    1. Dictionary
      Do you know how to set Russian dictionary. It does not hyphenate words. Thanks!
    2. ASP: Can Dictionary object store arrays or recordsets as items?
      Title says it all. Anyone know if the ASP/ VBScript dictionary object can store other collections -- say, arrays, recordsets, or other Dictionaries...
    3. Passing Dictionary object byref
      Passing Dictionary object byref Ive created an ASP class that uses a dictionary object which is filled from a recordset. It passes the object to...
    4. Can you return a dictionary object from a function?
      "Mark Schupp" <mschupp@ielearning.com> wrote in message news:OwPxqtxPDHA.1748@TK2MSFTNGP11.phx.gbl... doh!
    5. Dictionary Object Replacement
      Hi, Is there an equivalent object that I can use to replace the regular Dictionary object? I played with DictionaryBase and DictionaryEntry and...
  3. #2

    Default Re: Dictionary Object

    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$1@sparta.btinternet.com...
    > 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 Guest

  4. #3

    Default Re: Dictionary Object


    "Ray at <%=sLocation%>" <ask@me.forit> wrote in message
    news:O3txsYZRDHA.2248@TK2MSFTNGP12.phx.gbl...
    > 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 Guest

  5. #4

    Default Re: Dictionary Object

    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$1@hercules.btinternet.com...
    >
    > "Ray at <%=sLocation%>" <ask@me.forit> wrote in message
    > news:O3txsYZRDHA.2248@TK2MSFTNGP12.phx.gbl...
    > > 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 Guest

  6. #5

    Default Re: Dictionary Object


    "Ray at <%=sLocation%>" <ask@me.forit> wrote in message
    news:e7IwBlZRDHA.1924@TK2MSFTNGP12.phx.gbl...
    > 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 Guest

  7. #6

    Default Re: Dictionary Object

    "Johnny Klunk" <johnnyklunk@:rem0ve-this:johnnyklunk.com> wrote in
    message news:befbpm$47q$1@sparta.btinternet.com...
    >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 Guest

  8. #7

    Default Re: Dictionary Object

    "Johnny Klunk" <johnnyklunk@:rem0ve-this:johnnyklunk.com> wrote in
    message news:befga2$cga$1@sparta.btinternet.com...
    >
    > > 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 Guest

  9. #8

    Default Dictionary Object

    I have the following:
    sub readcsv
    dim csv_dsn, csv_filename, csv_oconn, csv_rs, csv_sql, itm
    csv_dsn = "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
    "Dbq=D:\ul_tmp\;Extensions=asc,csv,tab,txt;" & _
    "Persist Security Info=False"
    csv_filename = session("filename")
    set csv_oconn = server.createobject("ADODB.Connection")
    set csv_rs = server.createobject("ADODB.Recordset")
    csv_sql = "select * from " & csv_filename & ""
    csv_oconn.open csv_dsn
    set csv_rs = csv_oconn.execute(csv_sql)
    response.write "<select name=""userflds""><br>"
    response.write "<option value=""none"">none</option><br>"
    for each itm in csv_rs.fields
    response.write "<option value=""" & itm.name & """>" & itm.name &
    "</option><br>"
    next
    response.write "</select><br>"
    csv_rs.close
    csv_oconn.close
    set csv_oconn = nothing
    set csv_rs = nothing
    end sub

    That builds select list with available user fields. However, I need this
    to be loaded several times -- currently I just call the sub each time
    but that takes way too long. I'm looking to load this once, then
    populate a dictionary object and then use that to write out the select
    list each time it's required. Any examples on how I coudl do that? Is
    there a better way to do this?
    Dave Karmens Guest

  10. #9

    Default Re: Dictionary Object

    Instead of using a sub, make it a function that returns an array of items,
    or make it a function that returns an html string from "<select>" to
    "</select>." Then, you can do:


    sVals = readcsv()
    Response.Write sVals



    '''later on down the page
    Response.Write sVals


    Are you familiar with Sub vs. Function?

    Ray at work


    "Dave Karmens" <spam_just1coder@yahoo.ca> wrote in message
    news:epIuyfG$DHA.3712@tk2msftngp13.phx.gbl...
    > I have the following:
    > sub readcsv
    > dim csv_dsn, csv_filename, csv_oconn, csv_rs, csv_sql, itm
    > csv_dsn = "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
    > "Dbq=D:\ul_tmp\;Extensions=asc,csv,tab,txt;" & _
    > "Persist Security Info=False"
    > csv_filename = session("filename")
    > set csv_oconn = server.createobject("ADODB.Connection")
    > set csv_rs = server.createobject("ADODB.Recordset")
    > csv_sql = "select * from " & csv_filename & ""
    > csv_oconn.open csv_dsn
    > set csv_rs = csv_oconn.execute(csv_sql)
    > response.write "<select name=""userflds""><br>"
    > response.write "<option value=""none"">none</option><br>"
    > for each itm in csv_rs.fields
    > response.write "<option value=""" & itm.name & """>" & itm.name &
    > "</option><br>"
    > next
    > response.write "</select><br>"
    > csv_rs.close
    > csv_oconn.close
    > set csv_oconn = nothing
    > set csv_rs = nothing
    > end sub
    >
    > That builds select list with available user fields. However, I need this
    > to be loaded several times -- currently I just call the sub each time
    > but that takes way too long. I'm looking to load this once, then
    > populate a dictionary object and then use that to write out the select
    > list each time it's required. Any examples on how I coudl do that? Is
    > there a better way to do this?

    Ray at Guest

  11. #10

    Default Re: Dictionary Object

    Ah right. I was just looking up how to populate an array with a recordset.

    Ray at <%=sLocation%> [MVP] wrote:
    > Instead of using a sub, make it a function that returns an array of items,
    > or make it a function that returns an html string from "<select>" to
    > "</select>." Then, you can do:
    >
    >
    > sVals = readcsv()
    > Response.Write sVals
    >
    >
    >
    > '''later on down the page
    > Response.Write sVals
    >
    >
    > Are you familiar with Sub vs. Function?
    >
    > Ray at work
    >
    >
    > "Dave Karmens" <spam_just1coder@yahoo.ca> wrote in message
    > news:epIuyfG$DHA.3712@tk2msftngp13.phx.gbl...
    >
    >>I have the following:
    >>sub readcsv
    >>dim csv_dsn, csv_filename, csv_oconn, csv_rs, csv_sql, itm
    >>csv_dsn = "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
    >>"Dbq=D:\ul_tmp\;Extensions=asc,csv,tab,txt;" & _
    >>"Persist Security Info=False"
    >>csv_filename = session("filename")
    >>set csv_oconn = server.createobject("ADODB.Connection")
    >>set csv_rs = server.createobject("ADODB.Recordset")
    >>csv_sql = "select * from " & csv_filename & ""
    >>csv_oconn.open csv_dsn
    >>set csv_rs = csv_oconn.execute(csv_sql)
    >>response.write "<select name=""userflds""><br>"
    >>response.write "<option value=""none"">none</option><br>"
    >>for each itm in csv_rs.fields
    >>response.write "<option value=""" & itm.name & """>" & itm.name &
    >>"</option><br>"
    >>next
    >>response.write "</select><br>"
    >>csv_rs.close
    >>csv_oconn.close
    >>set csv_oconn = nothing
    >>set csv_rs = nothing
    >>end sub
    >>
    >>That builds select list with available user fields. However, I need this
    >>to be loaded several times -- currently I just call the sub each time
    >>but that takes way too long. I'm looking to load this once, then
    >>populate a dictionary object and then use that to write out the select
    >>list each time it's required. Any examples on how I coudl do that? Is
    >>there a better way to do this?
    >
    >
    >
    Dave Karmens Guest

  12. #11

    Default Re: Dictionary Object

    Ah, conventienly, the recordset object has a .GetRows method which will do
    just that.

    [url]http://msdn.microsoft.com/library/en-us/ado270/htm/mdmthgetrows.asp[/url]

    [url]http://msdn.microsoft.com/library/en-us/ado270/htm/mdmscadoapireference.asp[/url]

    Ray at work

    "Dave Karmens" <spam_just1coder@yahoo.ca> wrote in message
    news:e11U8VH$DHA.3536@tk2msftngp13.phx.gbl...
    > Ah right. I was just looking up how to populate an array with a recordset.
    >
    > Ray at <%=sLocation%> [MVP] wrote:
    >
    > > Instead of using a sub, make it a function that returns an array of
    items,
    > > or make it a function that returns an html string from "<select>" to
    > > "</select>." Then, you can do:
    > >
    > >
    > > sVals = readcsv()
    > > Response.Write sVals
    > >
    > >
    > >
    > > '''later on down the page
    > > Response.Write sVals
    > >
    > >
    > > Are you familiar with Sub vs. Function?
    > >
    > > Ray at work
    > >
    > >
    > > "Dave Karmens" <spam_just1coder@yahoo.ca> wrote in message
    > > news:epIuyfG$DHA.3712@tk2msftngp13.phx.gbl...
    > >
    > >>I have the following:
    > >>sub readcsv
    > >>dim csv_dsn, csv_filename, csv_oconn, csv_rs, csv_sql, itm
    > >>csv_dsn = "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
    > >>"Dbq=D:\ul_tmp\;Extensions=asc,csv,tab,txt;" & _
    > >>"Persist Security Info=False"
    > >>csv_filename = session("filename")
    > >>set csv_oconn = server.createobject("ADODB.Connection")
    > >>set csv_rs = server.createobject("ADODB.Recordset")
    > >>csv_sql = "select * from " & csv_filename & ""
    > >>csv_oconn.open csv_dsn
    > >>set csv_rs = csv_oconn.execute(csv_sql)
    > >>response.write "<select name=""userflds""><br>"
    > >>response.write "<option value=""none"">none</option><br>"
    > >>for each itm in csv_rs.fields
    > >>response.write "<option value=""" & itm.name & """>" & itm.name &
    > >>"</option><br>"
    > >>next
    > >>response.write "</select><br>"
    > >>csv_rs.close
    > >>csv_oconn.close
    > >>set csv_oconn = nothing
    > >>set csv_rs = nothing
    > >>end sub
    > >>
    > >>That builds select list with available user fields. However, I need this
    > >>to be loaded several times -- currently I just call the sub each time
    > >>but that takes way too long. I'm looking to load this once, then
    > >>populate a dictionary object and then use that to write out the select
    > >>list each time it's required. Any examples on how I coudl do that? Is
    > >>there a better way to do this?
    > >
    > >
    > >

    Ray at Guest

  13. #12

    Default Re: Dictionary Object

    Thanks Ray. I'll check those out.

    Ray at <%=sLocation%> [MVP] wrote:
    > Ah, conventienly, the recordset object has a .GetRows method which will do
    > just that.
    >
    > [url]http://msdn.microsoft.com/library/en-us/ado270/htm/mdmthgetrows.asp[/url]
    >
    > [url]http://msdn.microsoft.com/library/en-us/ado270/htm/mdmscadoapireference.asp[/url]
    >
    > Ray at work
    >
    > "Dave Karmens" <spam_just1coder@yahoo.ca> wrote in message
    > news:e11U8VH$DHA.3536@tk2msftngp13.phx.gbl...
    >
    >>Ah right. I was just looking up how to populate an array with a recordset.
    >>
    >>Ray at <%=sLocation%> [MVP] wrote:
    >>
    >>
    >>>Instead of using a sub, make it a function that returns an array of
    >
    > items,
    >
    >>>or make it a function that returns an html string from "<select>" to
    >>>"</select>." Then, you can do:
    >>>
    >>>
    >>>sVals = readcsv()
    >>>Response.Write sVals
    >>>
    >>>
    >>>
    >>>'''later on down the page
    >>>Response.Write sVals
    >>>
    >>>
    >>>Are you familiar with Sub vs. Function?
    >>>
    >>>Ray at work
    >>>
    >>>
    >>>"Dave Karmens" <spam_just1coder@yahoo.ca> wrote in message
    >>>news:epIuyfG$DHA.3712@tk2msftngp13.phx.gbl...
    >>>
    >>>
    >>>>I have the following:
    >>>>sub readcsv
    >>>>dim csv_dsn, csv_filename, csv_oconn, csv_rs, csv_sql, itm
    >>>>csv_dsn = "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
    >>>>"Dbq=D:\ul_tmp\;Extensions=asc,csv,tab,txt;" & _
    >>>>"Persist Security Info=False"
    >>>>csv_filename = session("filename")
    >>>>set csv_oconn = server.createobject("ADODB.Connection")
    >>>>set csv_rs = server.createobject("ADODB.Recordset")
    >>>>csv_sql = "select * from " & csv_filename & ""
    >>>>csv_oconn.open csv_dsn
    >>>>set csv_rs = csv_oconn.execute(csv_sql)
    >>>>response.write "<select name=""userflds""><br>"
    >>>>response.write "<option value=""none"">none</option><br>"
    >>>>for each itm in csv_rs.fields
    >>>>response.write "<option value=""" & itm.name & """>" & itm.name &
    >>>>"</option><br>"
    >>>>next
    >>>>response.write "</select><br>"
    >>>>csv_rs.close
    >>>>csv_oconn.close
    >>>>set csv_oconn = nothing
    >>>>set csv_rs = nothing
    >>>>end sub
    >>>>
    >>>>That builds select list with available user fields. However, I need this
    >>>>to be loaded several times -- currently I just call the sub each time
    >>>>but that takes way too long. I'm looking to load this once, then
    >>>>populate a dictionary object and then use that to write out the select
    >>>>list each time it's required. Any examples on how I coudl do that? Is
    >>>>there a better way to do this?
    >>>
    >>>
    >>>
    >
    >
    Dave Karmens Guest

  14. #13

    Default Re: Dictionary Object

    P.S. Typical code for using an array that is returned by the .GetRows
    method would be like:

    sSQL = "SELECT [ID],[TheName],[TheSomethingElse] FROM [TheTable] WHERE
    [TheColumn]='some value'"
    oADO.Open YourConnectionString
    Set rs = oADO.Execute(sSQL)
    If Not rs.EOF Then aVals = rs.GetRows()
    rs.Close : Set rs = Nothing
    oADO.Close : Set oADO = Nothing

    If Not IsEmpty(aVals) Then
    For i = 0 To UBound(aVals, 2)
    Response.Write "The ID is " & aVals(0,i) & "<br>"
    Response.Write "The name is " & aVals(1,i) & "<br>"
    Response.Write "The something else is " & aVals(2,i) & "<br>"
    Response.Write "<hr>"
    Next
    Else
    Response.Write "There was nothing"
    End If

    Unproofread!

    Ray at work



    "Ray at <%=sLocation%> [MVP]" <myfirstname at lane34 dot com> wrote in
    message news:%23mqBmYH$DHA.3852@TK2MSFTNGP10.phx.gbl...
    > Ah, conventienly, the recordset object has a .GetRows method which will do
    > just that.
    >
    > [url]http://msdn.microsoft.com/library/en-us/ado270/htm/mdmthgetrows.asp[/url]
    >
    >
    [url]http://msdn.microsoft.com/library/en-us/ado270/htm/mdmscadoapireference.asp[/url]
    >
    > Ray at work
    >
    > "Dave Karmens" <spam_just1coder@yahoo.ca> wrote in message
    > news:e11U8VH$DHA.3536@tk2msftngp13.phx.gbl...
    > > Ah right. I was just looking up how to populate an array with a
    recordset.
    > >
    > > Ray at <%=sLocation%> [MVP] wrote:
    > >
    > > > Instead of using a sub, make it a function that returns an array of
    > items,
    > > > or make it a function that returns an html string from "<select>" to
    > > > "</select>." Then, you can do:
    > > >
    > > >
    > > > sVals = readcsv()
    > > > Response.Write sVals
    > > >
    > > >
    > > >
    > > > '''later on down the page
    > > > Response.Write sVals
    > > >
    > > >
    > > > Are you familiar with Sub vs. Function?
    > > >
    > > > Ray at work
    > > >
    > > >
    > > > "Dave Karmens" <spam_just1coder@yahoo.ca> wrote in message
    > > > news:epIuyfG$DHA.3712@tk2msftngp13.phx.gbl...
    > > >
    > > >>I have the following:
    > > >>sub readcsv
    > > >>dim csv_dsn, csv_filename, csv_oconn, csv_rs, csv_sql, itm
    > > >>csv_dsn = "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
    > > >>"Dbq=D:\ul_tmp\;Extensions=asc,csv,tab,txt;" & _
    > > >>"Persist Security Info=False"
    > > >>csv_filename = session("filename")
    > > >>set csv_oconn = server.createobject("ADODB.Connection")
    > > >>set csv_rs = server.createobject("ADODB.Recordset")
    > > >>csv_sql = "select * from " & csv_filename & ""
    > > >>csv_oconn.open csv_dsn
    > > >>set csv_rs = csv_oconn.execute(csv_sql)
    > > >>response.write "<select name=""userflds""><br>"
    > > >>response.write "<option value=""none"">none</option><br>"
    > > >>for each itm in csv_rs.fields
    > > >>response.write "<option value=""" & itm.name & """>" & itm.name &
    > > >>"</option><br>"
    > > >>next
    > > >>response.write "</select><br>"
    > > >>csv_rs.close
    > > >>csv_oconn.close
    > > >>set csv_oconn = nothing
    > > >>set csv_rs = nothing
    > > >>end sub
    > > >>
    > > >>That builds select list with available user fields. However, I need
    this
    > > >>to be loaded several times -- currently I just call the sub each time
    > > >>but that takes way too long. I'm looking to load this once, then
    > > >>populate a dictionary object and then use that to write out the select
    > > >>list each time it's required. Any examples on how I coudl do that? Is
    > > >>there a better way to do this?
    > > >
    > > >
    > > >
    >
    >

    Ray at Guest

  15. #14

    Default Re: Dictionary Object

    Great!

    What if the sSQL is " SELECT * ", and the # columns is unknown?

    Ray at <%=sLocation%> [MVP] wrote:
    > P.S. Typical code for using an array that is returned by the .GetRows
    > method would be like:
    >
    > sSQL = "SELECT [ID],[TheName],[TheSomethingElse] FROM [TheTable] WHERE
    > [TheColumn]='some value'"
    > oADO.Open YourConnectionString
    > Set rs = oADO.Execute(sSQL)
    > If Not rs.EOF Then aVals = rs.GetRows()
    > rs.Close : Set rs = Nothing
    > oADO.Close : Set oADO = Nothing
    >
    > If Not IsEmpty(aVals) Then
    > For i = 0 To UBound(aVals, 2)
    > Response.Write "The ID is " & aVals(0,i) & "<br>"
    > Response.Write "The name is " & aVals(1,i) & "<br>"
    > Response.Write "The something else is " & aVals(2,i) & "<br>"
    > Response.Write "<hr>"
    > Next
    > Else
    > Response.Write "There was nothing"
    > End If
    >
    > Unproofread!
    >
    > Ray at work
    >
    >
    >
    > "Ray at <%=sLocation%> [MVP]" <myfirstname at lane34 dot com> wrote in
    > message news:%23mqBmYH$DHA.3852@TK2MSFTNGP10.phx.gbl...
    >
    >>Ah, conventienly, the recordset object has a .GetRows method which will do
    >>just that.
    >>
    >>[url]http://msdn.microsoft.com/library/en-us/ado270/htm/mdmthgetrows.asp[/url]
    >>
    >>
    >
    > [url]http://msdn.microsoft.com/library/en-us/ado270/htm/mdmscadoapireference.asp[/url]
    >
    >>Ray at work
    >>
    >>"Dave Karmens" <spam_just1coder@yahoo.ca> wrote in message
    >>news:e11U8VH$DHA.3536@tk2msftngp13.phx.gbl...
    >>
    >>>Ah right. I was just looking up how to populate an array with a
    >
    > recordset.
    >
    >>>Ray at <%=sLocation%> [MVP] wrote:
    >>>
    >>>
    >>>>Instead of using a sub, make it a function that returns an array of
    >>
    >>items,
    >>
    >>>>or make it a function that returns an html string from "<select>" to
    >>>>"</select>." Then, you can do:
    >>>>
    >>>>
    >>>>sVals = readcsv()
    >>>>Response.Write sVals
    >>>>
    >>>>
    >>>>
    >>>>'''later on down the page
    >>>>Response.Write sVals
    >>>>
    >>>>
    >>>>Are you familiar with Sub vs. Function?
    >>>>
    >>>>Ray at work
    >>>>
    >>>>
    >>>>"Dave Karmens" <spam_just1coder@yahoo.ca> wrote in message
    >>>>news:epIuyfG$DHA.3712@tk2msftngp13.phx.gbl.. .
    >>>>
    >>>>
    >>>>>I have the following:
    >>>>>sub readcsv
    >>>>>dim csv_dsn, csv_filename, csv_oconn, csv_rs, csv_sql, itm
    >>>>>csv_dsn = "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
    >>>>>"Dbq=D:\ul_tmp\;Extensions=asc,csv,tab,txt; " & _
    >>>>>"Persist Security Info=False"
    >>>>>csv_filename = session("filename")
    >>>>>set csv_oconn = server.createobject("ADODB.Connection")
    >>>>>set csv_rs = server.createobject("ADODB.Recordset")
    >>>>>csv_sql = "select * from " & csv_filename & ""
    >>>>>csv_oconn.open csv_dsn
    >>>>>set csv_rs = csv_oconn.execute(csv_sql)
    >>>>>response.write "<select name=""userflds""><br>"
    >>>>>response.write "<option value=""none"">none</option><br>"
    >>>>>for each itm in csv_rs.fields
    >>>>>response.write "<option value=""" & itm.name & """>" & itm.name &
    >>>>>"</option><br>"
    >>>>>next
    >>>>>response.write "</select><br>"
    >>>>>csv_rs.close
    >>>>>csv_oconn.close
    >>>>>set csv_oconn = nothing
    >>>>>set csv_rs = nothing
    >>>>>end sub
    >>>>>
    >>>>>That builds select list with available user fields. However, I need
    >
    > this
    >
    >>>>>to be loaded several times -- currently I just call the sub each time
    >>>>>but that takes way too long. I'm looking to load this once, then
    >>>>>populate a dictionary object and then use that to write out the select
    >>>>>list each time it's required. Any examples on how I coudl do that? Is
    >>>>>there a better way to do this?
    >>>>
    >>>>
    >>>>
    >>
    >
    >
    Dave Karmens Guest

  16. #15

    Default Re: Dictionary Object


    "Dave Karmens" <spam_just1coder@yahoo.ca> wrote in message
    news:uNNJ%23jH$DHA.2308@tk2msftngp13.phx.gbl...
    > Great!
    >
    > What if the sSQL is " SELECT * ", and the # columns is unknown?
    Well, in an ideal world, you wouldn't ever use SELECT *.
    ([url]http://www.aspfaq.com/show.asp?id=2096[/url]) But, you can know the number of
    columns by the UBound of the first dimension of the array.

    iNumberOfColumns = UBound(theArray, 1) + 1

    VBScript arrays are zero based, so the first column is 0, second 1, third 2,
    and so on. Actually knowing the number of columns doesn't really matter, as
    you could use code like:

    <%
    If Not IsEmpty(aVals) Then
    For i = 0 To UBound(aVals, 2)
    Response.Write "<tr>"
    For iColumns = 0 To UBound(aVals, 1)
    Response.Write "<td>" & aVals(iColumns, i) & "</td>"
    Next
    Response.Write "</tr>"
    Next
    Else
    Response.Write "There was nothing"
    End If
    %>

    Note that you could .GetString your recordset and just return a string of
    <tr><td>values</td></tr>s, without having to loop like this.

    Ray at work






    Ray at Guest

  17. #16

    Default Re: Dictionary Object

    I am only using SELECT * because the the file has an unknown # of
    columns...

    What I'm trying to do is write a little app that lets me upload a list
    into a database and also have the ability to map columns from the list
    to the columns in the database.

    For this part I only want the first row so I can show the available
    column names from the text file. So would not "select top 1" work?

    Then, another function that runs a " select * " to perform the import
    after the mapping is complete.


    Ray at <%=sLocation%> [MVP] wrote:
    > Well, in an ideal world, you wouldn't ever use SELECT *.
    > ([url]http://www.aspfaq.com/show.asp?id=2096[/url]) But, you can know the number of
    > columns by the UBound of the first dimension of the array.
    >
    > iNumberOfColumns = UBound(theArray, 1) + 1
    >
    > VBScript arrays are zero based, so the first column is 0, second 1, third 2,
    > and so on. Actually knowing the number of columns doesn't really matter, as
    > you could use code like:
    >
    > <%
    > If Not IsEmpty(aVals) Then
    > For i = 0 To UBound(aVals, 2)
    > Response.Write "<tr>"
    > For iColumns = 0 To UBound(aVals, 1)
    > Response.Write "<td>" & aVals(iColumns, i) & "</td>"
    > Next
    > Response.Write "</tr>"
    > Next
    > Else
    > Response.Write "There was nothing"
    > End If
    > %>
    >
    > Note that you could .GetString your recordset and just return a string of
    > <tr><td>values</td></tr>s, without having to loop like this.
    >
    > Ray at work
    >
    >
    >
    >
    >
    >
    Dave Karmens Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139