Vb.net Convert integer DBNULL to 0 - error

I have this method:

Private Function convertInteger(intInteger As Object) As Integer If IsDBNull(intInteger) Then convertInteger = 0 Else convertInteger = cInt(intInteger) End If End Function 

But it returns this error:

operator '=' is not defined for type 'integer' and type 'dbnull'

Im trying to convert a DBnull value to 0 ..

But the problem is that the im value trying to convert is not always DBnull .. so how should I handle this?

+4
source share
3 answers

try it

 Private Function convertInteger(intInteger As Object) As Integer If intInteger = DBNull.Value Then Return 0 End If Return intInteger End Function 

As suggested by [Tim Schmelter], look at the Nullable types

+2
source

try it

 Private Function convertInteger(ByVal intInteger As Object) as Integer If IsDBNull(intInteger) Then Return 0 Else Return CInt(intInteger) End if End Function 
+1
source
 Private Sub readValue() Dim cSql As String Dim oCnn as OleDbConnection Dim oCmd as OleDbCommand Dim oDataReader as OleDbDataReader Dim valor as Integer oCnn.open() cSql = "SELECT FIELD_NAME FROM TABLE" oCmd = New OleDbCommand(cSql, oCnn) oReader = oCmd.executeReader if oReader.HasRows then oReader.read() valor = IIf(IsDBNull(oDataReader("FIELD_NAME")), 0, oDataReader("FIELD_NAME")) End If End Sub 
0
source

Source: https://habr.com/ru/post/1434585/


All Articles