How to assign Null Values to Date Type Variable in .Net

Actually, you can’t assign a null value to a Date Type variable but you can check if the variable has been set or not by comparing its current value versus default value (#12:00:00 AM#) of date/datetime, you can get the default value by using

  1. Date.MinValue or DateTime.MinValue

To do this, let say you have a variable called LastUpdate with a data type of Datetime

  1. Dim LastUpdate  as DateTime
  2. dim dr as Datarow
  3.  
  4. If DateTime.MinValue = LastUpdate   Then
  5.         'Its Null value
  6.         'if you want to save the value to the database then use the DBNull.Value
  7.         dr("LastUpdate") = DBNull.Value
  8. Else
  9.         dr("LastUpdate") =LastUpdate  
  10. End If

, ,

  1. No comments yet.
(will not be published)
Submit Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Subscribe to comments feed
  1. No trackbacks yet.

SetPageWidth