ASP recordset to array

Posted on 2006-03-27
Last Modified: 2009-03-03
Im using asp and sql server for a web page

I've got a simple recordset:

I want to display the data in a series of select lists based on itemcategory. I could pull a different recordset for each category, but that seems way clunky.

How can I pull one recordset and distribute to the appropriate select lists? The selects should be built dynamically so one category, one select list. 8 (or whatever)categories, 8 select lists.
Question by:webdork
    LVL 2

    Accepted Solution


    Your query should look like this

    select itemid, itemname, itemcategory
    from tablename
    order by itemcategory

    Then in your asp page you should do something like this (pseudo code):

    while myReader.Read()

      if myReader("itemcategory") <> oldCategory then
        dynamicSelectList = new selectlist()
        dynamicSelectList.Add(myReader("itemname"), myReader("itemid"))
        oldCategory = myReader("itemcategory")
        dynamicSelectList.Add(myReader("itemname"), myReader("itemid"))
      end if


    Not sure exactly how you add a select box to a page dynamically using asp, but this should give you an idea of how to deal with it for SQL
    LVL 44

    Assisted Solution

    also, since Classic ASP (as opposed to ASP.NET) uses ADO as the Data Access Technology, after you have an ADO Recordset, you can use the Recordset.GetRows methood to create an Array of n Rows (one row for each record) and m columns (i column for each field in each record) or VARIANTS (so each colum can hold a different KIND of value:

    dim cn
    Dim rs
    Dim strSQL
    dim MyArr

    set cn = Server.CreateObject("ADODB.Connection")
    cn.ConnectionString = ....

    set rs = Server.CreateObject(ADODB.Recordset")
    strSQL = "select itemid, itemname, itemcategory from tablename order by itemcategory"

    rs.OpenRecordset(strSQL, cn)
    MyArr = rs.GetRows()

    now you can use this array for whatever purposes you need.


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now