4

I'm having 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 value im trying to convert is not always DBnull.. so how should i handle this?

MMM
  • 311
  • 5
  • 14
  • 30

3 Answers3

3

Try this

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 into Nullable types

codingbiz
  • 26,179
  • 8
  • 59
  • 96
  • 2
    Please don't suggest the old VB syntax `methodName=returnValue`, that works only for compatibility reasons. VB.NET works the same as C#, it has a return value: `return 0`. – Tim Schmelter Sep 17 '12 at 11:07
  • 1
    Am sorry for that. I didn't even see the function name – codingbiz Sep 17 '12 at 11:12
1

Try this

Private Function convertInteger(ByVal intInteger As Object) as Integer
   If IsDBNull(intInteger) Then
       Return 0
   Else
       Return CInt(intInteger)
   End if
End Function
Breeze
  • 2,010
  • 2
  • 32
  • 43
ahmed
  • 11
  • 1
1
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
Hadi GhahremanNezhad
  • 2,377
  • 5
  • 29
  • 58