0

Is there a way to force SqlDataReader to throw exceptions? There seems to be a very problematic behavior with it where SqlDataReader will have an exception and cause the function it's in to end prematurely without throwing an exception, specifically when it tries to read isDBNull values.

(5/10/2013) EDIT: On further reflection, it looks like be behavior of Microsoft.VisualBasic.CompilerServices.Conversions that is silently catching the exception.

And apparently the exception squelching only occurs while debugging--I ran a release build and the exception definitely surfaced there. So it's only a property of Visual Studio debugging apparently.

(5/14/2013) EDIT2: Found similar catching in debug without a try/catch block behavior with a SqlDataReader index not matching the column name. IndexOutOfRangeException only surfacing if "break on all exceptions" debug settings used.

More details:

Public Function GetData() As DataObj
    Dim sqlConn As New SqlConnection(ConnectionSettings.DBCon)
    Dim sqlCommand As New SqlCommand("an_sp", sqlConn)
    Dim sqlReader As SqlDataReader
    Dim dataObject As New DataObj

    sqlCommand.CommandType = CommandType.StoredProcedure

    sqlCommand.Connection.Open()
    sqlReader = sqlCommand.ExecuteReader

    While sqlReader.Read
        With dataObject
            'Exception thrown here in debugging
            .DateField = sqlReader("DateField")

        End With
    End While

    Return dataObject
End Function

System.InvalidCastException occurred Message="Conversion from type 'DBNull' to type 'Date' is not valid." Source="Microsoft.VisualBasic" StackTrace: at Microsoft.VisualBasic.CompilerServices.Conversions.ToDate(Object Value) at DataAccess.MyDAO.GetData() in C:\Users\me\Documents\Visual Studio 2008\VS 2008 Projects\General\DataAccess\DataAccess\MyDAO.vb:line 603 InnerException:

j.i.h.
  • 815
  • 8
  • 29

0 Answers0