ASP经典版-类型不匹配:'CInt'-简单问题 [英] ASP Classic - Type mismatch: 'CInt' - Easy question

查看:311
本文介绍了ASP经典版-类型不匹配:'CInt'-简单问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP classic中的类型转换存在问题.

Having an issue with type conversion in ASP classic.

这是我的代码:

        Set trainingCost = Server.CreateObject("ADODB.Recordset")
    strSQL3 = "SELECT cost1 FROM tblMain WHERE (Booked = 'Booked') AND (Paid IS NULL) AND (PaidDate BETWEEN '01/04/" & startyear & "' AND '31/03/" & endyear & "')"
    trainingCost.Open strSQL3, Connection
    trainingCost.movefirst
    totalTrainCost = 0
    do while not trainingCost.eof
        trainCost = trainingCost("cost1")
        If NOT isNull(trainCost) then
            trainCostStr = CStr(trainCost)
            trainCostStr = Replace(trainCostStr, "£", "")
            trainCostStr = Replace(trainCostStr, ",", "")
            totalTrainCost = totalTrainCost + CInt(trainCostStr)
        end if
        trainingCost.movenext
    loop 

    trainingCost.close

运行此命令时,出现以下错误:

when I run this I get the following error:

Microsoft VBScript运行时(0x800A000D) 类型不匹配:"CInt" /systems/RFT/v1.2/Extract.asp,第43行

Microsoft VBScript runtime (0x800A000D) Type mismatch: 'CInt' /systems/RFT/v1.2/Extract.asp, line 43

"totalTrainCost = totalTrainCost + CInt(trainCostStr)"

which is "totalTrainCost = totalTrainCost + CInt(trainCostStr)"

我猜测问题是与无法将String值广播到Int有关,在这种情况下,有什么方法可以捕获此错误?我还没有与asp classic一起工作过,所以任何帮助都将是有用的

Im guessing that the problem is to do with the String value being uncastable to Int in which case is there any way to catch this error? I havent worked with asp classic much so any help would be usefull

欢呼

-编辑-

cost1列的类型为String,因为它可能包含数字或字符序列,例如£10.00或TBC

the type of column cost1 is String as it may contain a number or a sequence of chars eg £10.00 or TBC

推荐答案

您有两种选择.您可以通过使用IsNumeric函数提前检查该值是否为数字来主动进行操作:

You have a couple of choices. You can be proactive by checking ahead of time whether the value is numeric using the IsNumeric function:

 If IsNumeric(trainCostStr) Then
    totalTrainCost = totalTrainCost + CInt(trainCostStr)
 Else
    ' Do something appropriate
 End If

...或者您可以通过使用错误捕获来做出反应;在Classic ASP中,可能最容易定义函数并使用On Error Resume Next:

...or you can be reactive by using error catching; in Classic ASP probably easiest to define a function and use On Error Resume Next:

Function ConvertToInt(val)
    On Error Resume Next
    ConvertToInt = CInt(val)
    If Err.Number <> 0 Then
        ConvertToInt = Empty
        Err.Clear
    End If
End Function

或者返回0或Null或任何适合您的值,然后在trainCost代码中使用它.

Or return 0 or Null or whatever suits you, then use it in your trainCost code.

请注意,CInt需要一个整数,并且将停在第一个非数字位置,因此"123.45"返回为123.请查看其他转换,如CDouble,CCur等.

Note that CInt expects an integer and will stop at the first non-digit, so "123.45" comes back as 123. Look at the other conversions, CDouble, CCur, etc.

这篇关于ASP经典版-类型不匹配:'CInt'-简单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆