ASP Classic - Type mismatch: 'CInt' - Easy question

后端 未结 4 1992
南方客
南方客 2021-01-19 14:26

Having an issue with type conversion in ASP classic.

heres my code:

        Set trainingCost = Server.CreateObject(\"ADODB.Recordset\")
    strSQL3 =         


        
4条回答
  •  别那么骄傲
    2021-01-19 14:47

    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
    

    ...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
    

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

    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.

提交回复
热议问题