Unbreakable
Unbreakable

Reputation: 8084

DBNull Exception from datatable row

I am a beginner and stuck at one place. So basically I need to set date value to one of the datarow. But when ever date is empty string I need to set it to "Now". But I am not able to do so. I am getting exception as System.Data.StrongTypingException' "The value for column 'dteEntry' in table is DBNull

Here is what I have

    Dim strTimeStamp as String = "" ' code taken out for brevity
' dteEntry is of Date datatype
Dim newRow As ABC.Dataset.dtCardRow = ret.NewdtCardRow()
    If (Date.TryParse(strTimeStamp, newRow.dteEntry)) Then

    else
    newRow.dteEntry = Now
    End If

Upvotes: 1

Views: 371

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460138

This is a strongly typed DataSet which auto generates code like the NewdtCardRow method that you have used above. Every property that is nullable has also a method to check if it's null, it's name is derived from the name of the property, something like IsEntryDate_Null, which you can use to check if the value is NULL. Otherwise you get an exception if you read that property as you do in Date.TryParse.

But in this case you can prevent this exception by using a local variable for Date.Parse instead of passing the property itself:

newRow.dteEntry = DateTime.Now ' writing doesnt cause this exception
Dim entryDate As Date
If Date.TryParse(strTimeStamp, entryDate) Then 
   newRow.dteEntry = entryDate ' writing doesnt cause this exception
End If

Upvotes: 1

Related Questions