vb.net 运算符 '=' 未为类型 'DBNull' 和类型 'Integer' 定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16071869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Operator '=' is not defined for type 'DBNull' and type 'Integer'
提问by Richard
I've been struggling for the past few hours and still can't get my head round this one . The issue I am having is when someone has been admitted the database updates giving them BedID , when I then try and discharge them I can't seem to set the BedID (In the database) to nothing . This is an issue as I need to be able to admit and discharge as many people as I can.
过去几个小时我一直在挣扎,但仍然无法理解这一点。我遇到的问题是,当有人被承认数据库更新给他们 BedID 时,当我尝试释放他们时,我似乎无法将 BedID(在数据库中)设置为 nothing 。这是一个问题,因为我需要能够接纳和释放尽可能多的人。
Sub Dis1_Click(ByVal s As Object, ByVal e As EventArgs) Handles Dis1.ServerClick
Dim time As String = Now().ToString("dd-MM-yyyy hh:mm:ss")
sql = "UPDATE Allocation SET BedID = NULL , DischargeDate =" + "'" + time + "'" + " WHERE BedID = 1 "
cmd = New OdbcCommand(sql, cn)
cmd.ExecuteNonQuery()
End Sub
Sub BedCheck1()
If r("BedID") = "" Then
Else
If r("BedID") = 1 Then
ba = s & "<tr><td>" & r("Surname") & "</td>" & "<td>" & r("Forename") & "/<td>" & "<td>" & r("AdmitDate") & "</td>" & "<td>" & r("DischargeDate") & "</td>" & "<td>" & r("comments") & "</td></tr>"
End If
End If
End Sub
Thanks!
谢谢!
回答by Kenneth
You need to do a DbNull-check before you read the value:
在读取值之前,您需要进行 DbNull 检查:
If Not IsDbNull(r("BedID")) Then
If r("BedID") = "" Then
Else If r("BedID") = 1 Then
ba = s & "<tr><td>" & r("Surname") & "</td>" & "<td>" & r("Forename") & "/<td>" & "<td>" & r("AdmitDate") & "</td>" & "<td>" & r("DischargeDate") & "</td>" & "<td>" & r("comments") & "</td></tr>"
End If
End If
Note that DbNull is a special case and you need to use the IsDbNull-function to prevent this error
请注意,DbNull 是一种特殊情况,您需要使用 IsDbNull 函数来防止此错误
回答by HikariNoIchigen
Try this: if r("BedID").ToString = " " then
试试这个:如果 r("BedID").ToString = " " 那么
End If
万一
回答by Muhammad Saeed
Well, Before you check data validation, you need to check is it DB null.
那么,在检查数据验证之前,您需要检查它是否为 DB null。
If Not IsDbNull(r("BedID")) Then
your code
End if