Nullable DateTime

Eels

New member
Joined
Dec 18, 2010
Messages
2
Programming Experience
Beginner
I'm working with an Access Database, and the below code keeps giving me the error that it can not set the field EersteVerschijning (even when I do fill in a date). This is a Nullable(Of DateTime) field, to read the fields from the database I checked for IsDBNull and set them to Nothing if that was the case, so I've tried writing Nothing to the database and DBNull.value but no luck so far? any ideas anyone? feel free to ask for more explanation too

Public Sub VoegToe(ByVal nKrant As bsKrant)
Dim pos As Integer

Try
Dim drv As DataRowView = dv.AddNew
With nKrant
drv("Titel") = .Titel
drv("EersteVerschijning") = .EersteVerschijning
drv("Frequentie") = .Frequentie
End With
drv.EndEdit()
AddHandler da.RowUpdated, AddressOf OnRowUpdated
da.Update(dsProject, "kranten")

pos = Zoek(drv("Krantnr").ToString, "Krantnr")
GaNaar(pos)

Catch ex As Exception
dsProject.Tables("kranten").Clear()
da.Fill(dsProject, "kranten")
Throw New System.Exception("dbKranten - Sub VoegToe" & vbCrLf & ex.Message)
End Try
End Sub
 
I can't see any specific use of nullable Dates in your code but, in general, your code should look something like this:
VB.NET:
nullableDate = If(myDataRow.IsNull("SomeColumn"), _
                  DirectCast(Nothing, Date?), _
                  CDate(myDataRow("SomeColumn")))
VB.NET:
myDataRow("SomeColumn") = If(nullableDate.HasValue, _
                             nullableDate.Value, _
                             CObj(DBNull.Value))
 
Thanks a lot man, that worked! And yeah, sorry about my unclear explanation. The property .EersteVerschijning reffers to a nullable datetime field, but that only made sense if you speak Dutch, my bad
 
Back
Top