Results 1 to 5 of 5

Thread: Date Problem - extracting from Dataset

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    5

    Date Problem - extracting from Dataset

    Hi again...

    I am having a problem with Date values that I am extracting from Dataset which I then use in a calculation - subtracting two dates to get the difference in days. The latter works when the dates are in the correct format.

    I am extracting the date from a dataset and passing to the variable for calculation - there is no UI involved.

    As you can see the date in the dataset is there in date time format. The variable though is 12:00:00 AM (effectively zero) - no matter what I do I can't get the row value assigned to the CountDateLast variable in dd/mm/yy format. CountDateLast is Dim As DateTime.
    Name:  Capture2.PNG
Views: 106
Size:  5.5 KB

    Any suggestions for a solution would be much appreciated.
    Thanks,
    Ian

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,430

    Re: Date Problem - extracting from Dataset

    So many people make their lives so difficult when it comes to dates because they over-think things. There is no format. You're trying to accomplish something that is impossible for a reason that doesn't exist.

    What you are seeing there is a representation of a DateTime value, not a DateTime value itself. DateTime values are stored as simple numbers internally so that would mean nothing to view. The value you're seeing for the CountDateLast variable is a DateTime with the value #1/01/0001 12:00:00 AM#. That is the default value for a DateTime, just as the default value for an Integer is 0 and a Double is 0.0. The value you're seeing for DuDataSet1.TableCount.Rows(0).Item(0) is #1/01/2015 12:00:00 AM#. If you want to assign the value from the DataSet to the variable then do it in EXACTLY the same way as you would any other assignment of any other value of any other type to any other variable, i.e. you're trying to solve a problem that doesn't exist. The code is EXACTLY as you'd expect:
    Code:
    CountDateLast = CDate(DuDataSet1.TableCount.Rows(0).Item(0))
    The CDate is required if you have Option Strict On, which you should, because the Item property of a DataRow is type Object and therefore cannot be assigned to a DateTime variable without a cast.

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    5

    Re: Date Problem - extracting from Dataset

    Thanks for the response..I may well be being very thick about this;

    My Code is exactly as you say. It is however part of an If Statement
    Code:
       If RowCount = Not "0" Then
                        'get the most recent  record
                        Me.TableCountTableAdapter.FillByTop(Me.DuDataSet1.TableCount)
                        'Set Date and Total Count of that record
                        TableCountDateLast = CDate(Me.DuDataSet1.TableCount.Rows(0).Item(0))
                        TableCountTotalLast = Str(Me.DuDataSet1.TableCount.Rows(0).Item(2))
                    End If
    As a thought I pasted this outside the Loop and it works.
    Code:
       TableCountDateLast = CDate(Me.DuDataSet1.TableCount.Rows(0).Item(0))
    Is there some restriction with the IF loop.

    regards, Ian

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,430

    Re: Date Problem - extracting from Dataset

    This:
    Code:
    If RowCount = Not "0" Then
    does not do what you think it does. Again, data types! If you want to work with a number then work with a number, NOT a String. Presumably what you actually want is this:
    Code:
    If RowCount <> 0 Then

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    5

    Re: Date Problem - extracting from Dataset

    Many thanks.

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