|
-
Apr 21st, 2011, 08:50 PM
#1
Thread Starter
Frenzied Member
[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.
-
Apr 21st, 2011, 11:44 PM
#2
Re: Date not staying as shortdate
Hi,
Try formatting the date using the ToString() method itself.
Example:
vb.net Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myDate As Date = DateAndTime.Now
MessageBox.Show(myDate.ToString("dd-MMM-yyyy")) ' 22-Apr-2011
MessageBox.Show(myDate.ToString("dd-MM-yyyy")) ' 22-04-2011
End Sub
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,...
-
Apr 22nd, 2011, 03:31 AM
#3
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:
Dim myDate = CDate(view(i)("PaymentDate"))
Now that you have an actual Date variable, you can call ToShortDateString.
-
Apr 22nd, 2011, 07:19 AM
#4
Thread Starter
Frenzied Member
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.
-
Apr 22nd, 2011, 07:42 AM
#5
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.
 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.
-
Apr 22nd, 2011, 07:54 AM
#6
Thread Starter
Frenzied Member
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.
-
Apr 22nd, 2011, 08:04 AM
#7
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.
-
Apr 22nd, 2011, 08:08 AM
#8
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|