1

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
9
  • 2
    This question lacks detail for a good answer. What is .strTimeStamp and what is newRow.dteEntry... Commented Jun 9, 2017 at 15:00
  • ...also Strings are not dates. Other tips omitted for brevity. Commented Jun 9, 2017 at 15:02
  • made some edits. Commented Jun 9, 2017 at 15:03
  • @Codexer: newRow is basically a datarow for a table. Commented Jun 9, 2017 at 15:05
  • No this does not work. To be honest, everything is working fine Syntax wise and all. Only problem is when empty string comes I get the exception. Commented Jun 9, 2017 at 15:09

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Worked like a charm. I am beginner. Actually, I do have a property to check NULL for the datarow column. Can you kindly tell me if I was suppose to go that way. How to go about it. Suppose I don't want to create extra variable.
Can you put one small snippet which uses NULL auto generated property of the datarow column

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.