我有一个像这样的If子句:
If pkgBasedOn = "Area" Then
sql = "select F_Pkg from dbo.T_Exhibitor Where [F_Exhibitor_Code] = '" & cmb_Exhibitor.SelectedValue.ToString & "'"
cmd = New SqlCommand(sql, conn)
dr = cmd.ExecuteReader
If dr.Read Then
pkgCount = IIf(dr(0).ToString() = "", "", dr(0).ToString())
End If
dr.Close()
Else
pkgCount = 1
End If
每当F_Pkg值为null时,我都会收到以下错误:
“从字符串转换”到“整数类型无效”
答案 0 :(得分:1)
我不这样做这个错误是因为null
因为null.ToString()
会给你""
,但是如何将""
分配给integer
?所以你应该为此做一些转换。
pkgCount =Val(IIf(dr(0).ToString() = "", "", dr(0).ToString()))
如果字符串为0
""
答案 1 :(得分:0)
首先,
If dr.Read Then
pkgCount = IIf(dr(0).ToString() = "", "", dr(0).ToString())
else
//print an error message like ' No item found!!' or something up to your wish
End If
然后,使用try catch环绕以捕获错误
try{
//your code
}
catch(Exception e){
e.getmessage();
}