Results 1 to 8 of 8

Thread: [RESOLVED] Date not staying as shortdate

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Resolved [RESOLVED] Date not staying as shortdate

    I am making a small bill manager program. I have setup a listview and I am using access db. Things are working great and when I pay a bill it enters in the database perfect, but when it is shown in the listview it shows the date plus the time. Why does it show correct in the database and not in the listview? I tried using .ToShortDateString instead .ToString but then it just tells me it doesn't like late binding.
    Code:
    Public Sub LoadDataGrid()
              
            Dim i As Integer
            Dim strImageKey As Integer
            'Dim CurrentMonth As String = CStr(Date.Now)
            Try
                'connection.Open()
                If connection.State = ConnectionState.Closed Then
                    connection.Open()
                End If
                Me.table.Clear()
                Me.adapter.Fill(Me.table)
    
                ' Create a DataView with the table.
                Dim view As New DataView(Me.table)
                Dim x As Decimal
                Dim y As Decimal
                LblBillCount.Text = CStr(view.Count)
                For i = 0 To view.Count - 1
                    Me.LvBills.BeginUpdate()
    
                    If CBool(view(i)("isPaid")) = True Then
                        strImageKey = 0
                    Else
                        strImageKey = 1
                    End If
                    LvBills.Items.Add(view(i)("BillName").ToString, view(i)("BillName").ToString, strImageKey)
                    LvBills.Items(i).SubItems.Add(view(i)("PaymentDate").ToString)
                    LvBills.Items(i).SubItems.Add("$" & view(i)("Amount").ToString)
                    LvBills.Items(i).SubItems.Add("$" & view(i)("Balance").ToString)
                    ' add id to tag so we can call it later 
                    LvBills.Items(i).Tag = CStr(view(i)("BillName"))
                    Me.LvBills.EndUpdate()
                    x = x + CDec(view(i)("Amount"))
                    y = y + CDec(view(i)("Balance"))
    
                Next i
                LblPayTotal.Text = "$" & String.Format("{0:C2}", CStr(CDec(x)))
                LblBalanceTotal.Text = "$" & String.Format("{0:C2}", CStr(CDec(y)))
                        
    
            Catch ex As Exception
                Debug.WriteLine(ex.Message)
            End Try
    
    
         
        End Sub
    the bold line is where I tried .ToShortDateString and it errors saying late binding. I just want to know why the late binding and also why the hell it is showing the time after the date when it is not like that in the database.

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Date not staying as shortdate

    Hi,

    Try formatting the date using the ToString() method itself.

    Example:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim myDate As Date = DateAndTime.Now
    5.  
    6.         MessageBox.Show(myDate.ToString("dd-MMM-yyyy")) ' 22-Apr-2011
    7.         MessageBox.Show(myDate.ToString("dd-MM-yyyy"))  ' 22-04-2011
    8.     End Sub
    9. End Class

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Date not staying as shortdate

    You have a DataView. You are getting an item from that by index, which returns a DataRowView. Each field in a DataRowView can hold any data at all, so the Item property that you're using is type Object. The Object class doesn't have a ToShortDateString method. For the same reason, you won't be able to follow akhileshbc's advice, because the Object class has no ToString method that accepts a format string. You want to call a method of the Date type so you have to call it on that type, which means casting your Object reference, e.g.
    vb.net Code:
    1. Dim myDate = CDate(view(i)("PaymentDate"))
    Now that you have an actual Date variable, you can call ToShortDateString.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Date not staying as shortdate

    Thanks jm, that explains the late binding, but still doesn't explain where it came from if the date in the database is short to begin with.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Date not staying as shortdate

    A .NET DateTime value has no format. It's not short or long or anything in between. It's just a number.
    Quote Originally Posted by MSDN
    Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar (excluding ticks that would be added by leap seconds). For example, a ticks value of 31241376000000000L represents the date, Friday, January 01, 0100 12:00:00 midnight. A DateTime value is always expressed in the context of an explicit or default calendar.
    To display it, that number must be converted to a String and, in doing so, you can use any format you want. Regardless, every .NET DateTime value represents a point in time, so it always represents both a date and a time. What may be stored in your database is irrelevant to that.

    That said, I very much doubt that the date stored in your database is in a specific short format either. That is most likely also just a visual representation, with the actual data stored as a number.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Date not staying as shortdate

    Then the date shown to me in the database is wrong? Even though I set it as a shortdate in Access design?

    I understand how time is always with the date, but if you set things to show shortdate then it better well set it as that. I guess my problem is that data shown in vb.net is not exactly what is shown in access, and that is wrong. regardless how you look at it. The data should not change form one point to another.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Date not staying as shortdate

    Run this code:
    Code:
    Dim dt As Date = Date.Now
    
    MessageBox.Show(dt.ToLongDateString())
    MessageBox.Show(dt.ToShortDateString())
    MessageBox.Show(dt.ToLongTimeString())
    MessageBox.Show(dt.ToShortTimeString())
    MessageBox.Show(dt.ToString("yyyy-MMM-d HH:mm:ss"))
    Did the data change? Obviously not. There's only one Date value. Even so, all five messages looked different. Can you accept that it's possible to display the same value in multiple ways? The fact that Access displays dates to you in short format is irrelevant to everything except how those dates get displayed to you. They aren't stored in the database in short format. They are stored in the database as a number, which doesn't have any format at all. How slow do you think a database would be if it had to sort a column of dates and they were all strings in a particular format? Dates can be sorted easily because they are all numbers. When you query your database, the numbers in the database are used to create .NET Date objects, which are also numbers. There is no format. It's only when you display the value to the user that format is an issue, and how could the way the values are displayed by Access have any effect on that? It's your app. You decide how dates get displayed to the user.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Date not staying as shortdate

    I guess that makes since. And yes, it is all how we present it at the end.

    Thanks for the explanation

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