Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator '=' is not defined for type 'DBNull' and type 'Integer'

Tags:

vb.net

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.

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!

like image 715
Richard Avatar asked Apr 17 '13 23:04

Richard


People also ask

What is DBNull type?

The DBNull class represents a nonexistent value. In a database, for example, a column in a row of a table might not contain any data whatsoever. That is, the column is considered to not exist at all instead of merely not having a value. A DBNull object represents the nonexistent column.

What does conversion from type DBNull to type string is not valid mean?

It just means that the item has no value, so you need to test for DBNull before trying to add it to the ListView .


2 Answers

You need to do a DbNull-check before you read the value:

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

like image 66
Kenneth Avatar answered Oct 12 '22 23:10

Kenneth


Try this: if r("BedID").ToString = " " then

End If

like image 27
HikariNoIchigen Avatar answered Oct 12 '22 22:10

HikariNoIchigen