When i take up the listview that receives from database i can se the date i saved, but i can also se the time after the date, in the database i use shortdate to save the date in but when i se it in listview i have date and time se image.
What do i need to do, to get only the date (Shortdate) i have saved in the database to se in listview and not the time?
The code i use.
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListView1.View = View.Details
ListView1.Columns.Add("Pocket", 35)
ListView1.Columns.Add("SgDb", 85)
ListView1.Columns.Add("PocketNr", 85)
ListView1.Columns.Add("Year", 85)
ListView1.Columns.Add("BoughtDate", 85)
ListView1.Columns.Add("Cost", 85)
ListView1.Columns.Add("Own", 85)
Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=false;Data Source=|DataDirectory|\Database\dicp.accdb")
conn.Open()
Dim cmd As New OleDbCommand("Select Pocket, Sgdb, PocketNr, Year, BoughtDate, Cost, Own from dicp", conn)
Dim da As OleDbDataReader
da = cmd.ExecuteReader
ListView1.Items.Clear()
Do While da.Read = True
Dim list1 = ListView1.Items.Add(da(0))
list1.Subitems.Add(da(1))
Dim objitem2 As Object = da.GetValue(2)
list1.Subitems.Add(If(objitem2 IsNot Nothing, objitem2.ToString, ""))
Dim objitem3 As Object = da.GetValue(3)
list1.Subitems.Add(If(objitem3 IsNot Nothing, objitem3.ToString, ""))
Dim objitem4 As Object = da.GetValue(4)
list1.Subitems.Add(If(objitem4 IsNot Nothing, objitem4.ToString, ""))
Dim objitem5 As Object = da.GetValue(5)
list1.Subitems.Add(If(objitem5 IsNot Nothing, objitem5.ToString, ""))
Dim objitem6 As Object = da.GetValue(6)
list1.Subitems.Add(If(objitem6 IsNot Nothing, objitem6.ToString, ""))
Loop
conn.Close()
End Sub
.NET doesn't have shortdate, so it will be a DateTime without a time component (i.e. 12 AM)
DateTime.ToString defaults to show the time. So just provide a format string like this
Using conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=false;Data Source=|DataDirectory|\Database\dicp.accdb")
conn.Open()
Using cmd As New OleDbCommand("Select Pocket, Sgdb, PocketNr, Year, BoughtDate, Cost, Own from dicp", conn)
Using reader = cmd.ExecuteReader()
ListView1.Items.Clear()
Do While reader.Read()
Dim list1 = ListView1.Items.Add(reader("Pocket").ToString())
list1.SubItems.Add(reader("Sgdb")?.ToString())
list1.SubItems.Add(reader("PocketNr")?.ToString())
list1.SubItems.Add(reader("Year")?.ToString())
list1.SubItems.Add(String.Format("{0:yyyy/MM/dd}", If(reader("BoughtDate") IsNot Nothing, reader("BoughtDate"), "")))
list1.SubItems.Add(reader("Cost")?.ToString())
list1.SubItems.Add(reader("Own")?.ToString())
Loop
End Using
End Using
End Using
Also use Using blocks for disposable resources. And add Option Strict On at the top of your code to see that you need to convert some objects to strings to add to the ListView. Using the string column name as an indexer is much clearer than int indices. Use null conditional puts your null check inline with property access.