编辑从记录集中抓取的数据

时间:2012-07-19 12:58:39

标签: html asp-classic

是否可以编辑从记录集中获取的数据?在我的情况下,我试图将数量加在一起,这样我就可以获得总数。所以我要做的一个例子是:

<% 
   set rs = server.CreateObject("ADODB.recordset")
   totalqty = 0

   do NOT while rs.EOF
       totalqty = totalqty + rs("QTY")
   loop
>%

每当我尝试做这样的事情时,我总会得到一个'Type MisMatch'错误,我不知道如何解决这个问题。

与往常一样,任何和所有帮助都将受到赞赏。

2 个答案:

答案 0 :(得分:1)

尝试“转换”记录集中的值,如下所示:

CDbl( rs.fields("QTY").value )

这会将值转换为double。如果该值为null,则会出现错误,因此您必须首先检查...

或者您可以编写一个函数来始终获得正确的类型:

public function parse(value, alternative)
    dim val
    val = trim(value & "")
    parse = alternative
    if val = "" then exit function
    on error resume next
    select case varType(parse)
        case 2, 3 'integer, long
            parse = cLng(val)
        case 4, 5 'single, double
            parse = cdbl(val)
        case 6  'currency
            parse = ccur(val)
        case 7 'date
            parse = cDate(val)
        case 11 'bool
            parse = cBool(val)
        case 8 'string
            parse = value & ""
        case else
            on error goto 0
            lib.throwError("type not supported. val:" & value & " alt:" & alternative)
    end select
    on error goto 0
end function

dim val : val = rs("QTY")
val = parse(val, 0)

' now val is always an integer (either the value from db or 0)

答案 1 :(得分:0)

ulluoink的解决方案可行,但这更简单......

function ToDbl(vIn, nDefault)
    'Convert a variant to an integer using default where necessary
    if isnull(vIn) then
        ToDbl = nDefault
    else
        if IsNumeric(CStr(vIn)) Then
            ToDbl = CDbl(vIn)
        else
            ToDbl = nDefault
        end if
    end if
end function

然后打电话:

totalqty = totalqty + ToDbl(rs("QTY"), 0)
相关问题