PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
DateTime Compare Nullable-VBForums
Results 1 to 3 of 3

Thread: DateTime Compare Nullable

  1. #1

    Thread Starter
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017

    DateTime Compare Nullable

    Hello folks, It's been a while, hope your all doing well.

    I've been crawling around the rabbit hole of nullable types and am wondering if anyone can simplify this procedure. The incoming date from DataRow1 can be NULL or EMPTY , the datetime needs to be compared to DataRow2 which could also be NULL or EMPTY. If it is different then needs inserted into DataRow2 as DataRow1 Value and as DBNull.Value if DataRow1 is either NULL or EMPTY

    Dim Date1 As Nullable(Of DateTime)
    If Not DataRow1.Item("Date1") Is DBNull.Value OrElse
           Not String.IsNullOrEmpty(DataRow1.Item("Date1").ToString) Then
        Date1 = Convert.ToDateTime(DataRow1.Item("Date1"))
    End If
    Dim Date2 As Nullable(Of DateTime)
        'DataRow2 is type Datarow and = DataRow1.GetChildRows("RelationName").First
    If Not DataRow2.Item("Date2") Is DBNull.Value OrElse
           Not String.IsNullOrEmpty(DataRow2.Item("Date2").ToString) Then
        Date2 = Convert.ToDateTime(DataRow2.Item("Date2"))
    End If
          'For logging (Not relevent)
    Dim Date1Str As String = If(Date1 Is Nothing, String.Empty, Date1.ToShortDateString)
    Dim Date2Str As String = If(Date2 Is Nothing, String.Empty, Date2.ToShortDateString)
    If Date1.GetValueOrDefault <> Date2.GetValueOrDefault Then
           'Sub that creates log (not relevent)
        Notify("Table2-Date2 changed to value in Table1-1Date1", Date1Str, Date2Str)
        DataRow2.Item("Date2") = If(Date1 Is Nothing, DBNull.Value, Date1)
    End If

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: DateTime Compare Nullable

    This makes no sense: Code:
    1. If Not DataRow1.Item("Date1") Is DBNull.Value OrElse
    2.        Not String.IsNullOrEmpty(DataRow1.Item("Date1").ToString) Then
    The result of calling ToString on a DBNull object is an empty String, so there's no point testing for DBNull.Value and for whether calling ToString is an empty String. Also, the result of calling ToString can never be Nothing because every object will produce a String object when you call ToString and if the DataRow contains Nothing in the first place then ToString will throw a NullReferenceException. It seems like you have a DataTable that contains String data that represents dates but could also be DBNull.Value or Nothing. That is absolutely terrible to begin with. Where is this data coming from and is there any way you can avoid such a situation in the first place?

  3. #3

    Thread Starter
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017

    Re: DateTime Compare Nullable

    The data comes from a cloud based HR service that offers no validity, or restraint. You can literally type the word hippo into a date field. You're right, it is terrible. I have a few other words I can throw at it, but we will leave it at terrible. It a bit of a nightmare. Many cases to remove a date HR will put a space... this makes me want to kick a puppy (not really).

    I've start on a different approach, after getting the XML from this amazingly terrible cloud app Im going to iterate the rows and set the date value to DBNull.Value where a valid datetime cant be parsed. I supose this thread could be deleted.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width