VB为什么是DBnull?没有为字符串“11106.9"定义运算符“*".并输入“DBNull" [英] VB why DBnull? Operator '*' is not defined for string "11106.9" and type 'DBNull'

查看:31
本文介绍了VB为什么是DBnull?没有为字符串“11106.9"定义运算符“*".并输入“DBNull"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想计算数据库访问的百分比.这是我的代码

i just want calculate amount with percent on database access. this my code

 xlWorksheet.Range("AI" & CurrentRow2).Value = Format(CDbl(strGetAccrualAllocation*ADORS_Temp1.Fields("PERCENTAGE").Value).ToString("N2"))

这个错误响应

System.InvalidCastException: 没有为字符串定义运算符*"11106.9"并输入DBNull".

System.InvalidCastException: Operator '*' is not defined for string "11106.9" and type 'DBNull'.

谢谢

推荐答案

检查计算中使用的数据是否为 ​​NULL 总是明智的.我经常使用内联 if 来做到这一点,例如:

It is always wise to check data used in a calculation for NULL. I often use the inline if to do that, for example:

   IIF(IsDBNull(MyValue),0,MyValue)

如果 MyValue 为 null,则返回零,否则返回 MyValue 的值.

If MyValue is null, zero is returned, if not, the value of MyValue is returned.

或者如果你需要向用户表明一个值为空:

Or if you need to indicate to the user that a value is null:

If IsDBNull(MyValue) Then
    'message to user that vaslue is null or some other action
Else
    'perforn calculation
End If

当使用一串数字字符时,会发生从字符串到值的隐式转换.如果字符串可能包含数字字符以外的任何内容,则应进行测试,否则将生成另一个错误.

When using a string of numeric characters, implicit conversion from string to value will take place. If there is any chance that the string could contain anything other than numeric characters, you should test for that, otherwise another error will be generated.

这篇关于VB为什么是DBnull?没有为字符串“11106.9"定义运算符“*".并输入“DBNull"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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