Classic ASP For Each Loop terminating too early

2019-08-28 23:26发布

I have a very odd problem with one of my ASP scripts. It is a simple script, reads information from a database into a recordset and loops through the recordset, each time outputting HTML as a table row.

I'm having an issue where periodically it gets close to the end of its for each loop and it just stops without getting through all the recordsets. I know it is stopping because my resulting HTML only goes as far down as S or T in the recordset. The script is not crashing because underneath the for each loop I end the table and all that HTML is still present.

The odd thing is it is broken one refresh, then the next time I refresh it works - the data in the SQL Database is static and the ASP script is not changing. One load it can work, the next it can break.

I have no idea what is going on here so any advice is welcome!

1条回答
对你真心纯属浪费
2楼-- · 2019-08-28 23:34

One thing I tend to do is write the SQL to the page so I can see what's going on. Also, it might be helpful to write the count of the recordset to the page for debugging.

Try using something like this:

const C_NO_DATA             = "NO_DATA"
const C_ERROR                 = "ERROR"
const C_COL_IDENTIFIER        = 0
const C_COL_ERROR_ID        = 1
const C_COL_ERROR_MESSAGE    = 2
const C_COL_SQL                = 3
const C_COL_CONNECTION        = 4

function GetDataSet(sqlString, connString)
    'Initialise...
    dim returnVal, rsData
    on error resume next
        'Define and open the recordset object...
        set rsData = Server.CreateObject("ADODB.RecordSet")
        rsData.Open sqlString, connString, 0, 1, 1
        'Initialise an empty value for the containing array...
        redim returnVal(0,0)
        returnVal(0,0) = C_NO_DATA
        'Deal with any errors...
        if not rsData.EOF and not rsData.BOF then
            'Store the data...
            returnVal = rsData.GetRows()
            'Tidy up...
            rsData.close
            set rsData = nothing
            select case err.number
                case 3021    'No data returned
                    'Do nothing as the initial value will still exist (C_NO_DATA)
                case 0        'No error
                    'Do nothing as data has been returned
                case else
                    redim returnVal(4,0)
                    returnVal(C_COL_IDENTIFIER,0) = C_ERROR
                    returnVal(C_COL_ERROR_ID,0) = err.number
                    returnVal(C_COL_ERROR_MESSAGE,0) = err.description
                    returnVal(C_COL_SQL,0) = sqlString
                    returnVal(C_COL_CONNECTION,0) = connString
            end select
        end if
    on error goto 0
    'Return the array...
    GetDataSet = returnVal
end function

This routine will read the data directly into an array so you can examine it at leisure.

-- EDIT --

Just to add to this, the reason I've provided the code like this is as a function to extract all of the data in one go rather than loop through an open database connection.

查看更多
登录 后发表回答