I am checking a field for DBNull value using IsDBNull(field), but the statement never gets executed. When I press F11 to execute the line in Debug, it immediately jumps to the code in the DataSet.Designer.vb and stops at the field property statement after the catch e statement (see code below ).
Public Property T3d() As String
Get
Try
Return CType(Me(Me.tableStudentSchedule.T3dColumn),String)
Catch e As Global.System.InvalidCastException
Throw New Global.System.Data.StrongTypingException("The value for column 'T3d' in table 'StudentSchedule' is DBNull.", e)
End Try
End Get
Set
Me(Me.tableStudentSchedule.T3dColumn) = value
End Set
End Property
The e.message is "Conversion from type 'DBNull' to type 'String' is not valid."
Since this is auto-generated code, I haven't changed it.
I've tried checking the field for dbnull several different ways including using the following function.
Public Function NotNull(Of T)(ByVal Value As T, ByVal DefaultValue As T) As T
If Value Is Nothing OrElse IsDBNull(Value) Then
Return DefaultValue
Else
Return Value
End if
End Function
It never gets to the function because it goes directly to the DataSet.Designer.vb and stops at the field property after the catch e statement
This is the code with all of the IsDBNull checks in the same code. None of them work the way I expect them to work.
Do
Dim schDateRow As TmsDatabaseDataSet.StudentScheduleRow = TmsDatabaseDataSet.StudentSchedule.FindBySchDate(DtCurrent)
Dim pschDateRow As TmsDatabaseDataSet.StudentScheduleRow = TmsDatabaseDataSet.StudentSchedule.FindBySchDate(DtPrevious)
schDateRow.Hlights = NotNull((pschDateRow.Hlights), "")
schDateRow.T1 = NotNull(pschDateRow.T1, "")
schDateRow.T2 = NotNull(pschDateRow.T2, "")
schDateRow.T2d = NotNull(pschDateRow.T2d, "")
schDateRow.T3 = NotNull(pschDateRow.T3, "")
schDateRow.T3d = NotNull(pschDateRow.T3d, String.Empty)
If IsDBNull(pschDateRow.T3d) Then
schDateRow.T3d = String.Empty
Else
schDateRow.T3d = pschDateRow.T3d
End If
If Not DBNull.Value.Equals(pschDateRow.T3d) Then
schDateRow.T3d = pschDateRow.T3d
Else
schDateRow.T3d = String.Empty
End If
schDateRow.T3d = If(Not DBNull.Value.Equals(pschDateRow.T3d), String.Empty, pschDateRow.T3d)
schDateRow.T3d = If(IsDBNull(pschDateRow.T3d), String.Empty, pschDateRow.T3d)
DtCurrent = DtPrevious
DtPrevious = DateAdd(DateInterval.Day, -7, DtPrevious)
Loop While DtPrevious >= DtFrom
I've searched every article I can find to find out why it doesn't work. I check the field for DBNull before using it, but it never gets past the the IsDBNull(field) check.
By the way, I'm using Visual Studio 2013, since 2012 crashes all the time.
One more thing. This data fills a datagridview, including the record with the DBNull, so it fills the DataGridview properly with the DBNull value. I thought of doing the change in the DataGRidView, but this SHOULD work.
Thanks for any help.