ASP Classic - 类型不匹配:'CInt' - 简单问题 [英] ASP Classic - Type mismatch: 'CInt' - Easy question
问题描述
在 ASP 经典中遇到类型转换问题.
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)"
我猜这个问题与不能转换为 Int 的 String 值有关,在这种情况下有什么方法可以捕获这个错误?我没有使用过 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 列的类型是字符串,因为它可能包含一个数字或一系列字符,例如 £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
...或者您可以通过使用错误捕获来做出反应;在经典 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 Classic - 类型不匹配:'CInt' - 简单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!