Trying to add date column to listview
I am trying to retrieve data from a table in MS Access with VisualBasic. When the Date data type is set to date/time in access, the queries run fine, but I receive an error and it will not populate the listview. When I change the data type to text it will populate the listview, but the queries return the wrong information.
-------------------------------------------------------
Error:
An unhandled exception of type
'System.Reflection.AmbiguousMatchException' occured in
microsoft.visualbasic.dll
Additional information: No accessible overloaded
'ListViewItemCollection.Add' can be called without a narrowing conversion.
--------------------------------------------------------
Dim cnnConnection As New OleDb.OleDbConnection
cnnConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database.mdb"
cnnConnection.Open()
Dim strSQL As String = "SELECT * FROM TableName ORDER BY Date DESC;"
Dim adpDataAdapter As New OleDb.OleDbDataAdapter(strSQL, cnnConnection)
cnnConnection.Close()
Dim dstDataSet As New DataSet
adpDataAdapter.Fill(dstDataSet, "TableName")
Dim rowDataRow As DataRow
Dim litListViewItem As ListViewItem
For Each rowDataRow In dstDataSet.Tables("TableName").Rows
litListViewItem = lvwListView.Items.Add(rowDataRow("Date")
itListViewItem.SubItems.Add(rowDataRow("Item1"))
itListViewItem.SubItems.Add(rowDataRow("Item2"))
itListViewItem.SubItems.Add(rowDataRow("Item3"))
Next
Status:
Open Apr 24, 2007 - 03:37 PM
Visual Basic .NET, MS Access
5answers
Answers
Apr 25, 2007 - 01:15 AM
I dont know if it will work with vb.net but u can tweak the SQL that u use and format the date
eg
Dim strSQL As String = "SELECT f1, f2, f3, format(f4,'Short Date') FROM TableName ORDER BY f4 DESC;"
Apr 25, 2007 - 01:32 AM
yes it should work, I tried it in C#
Apr 25, 2007 - 01:32 AM
I used OleDbCommand and OleDbDataReader to read it and treat the datefield as a string
i.e.
OleDbCommand oleCmd = new OleDbCommand("SELECT format(mydtefld,'short date') FROM tblDates", oleConn);
and gettting that value worked like this
Console.WriteLine(oleRead.GetString(0).ToString());
so yes, u should be able to use the format command. It does mean u have to specify each column in your query though
Apr 26, 2007 - 12:25 PM
One note: oleRead.GetString(0) returns a string, so why do you add a .ToString() to it?
Cheers
Peter
Apr 26, 2007 - 02:45 PM
Thanks!
Answer this question
Share Your Own Experience & Expertise
We look to ensure that every question is answered by the best people with relevant expertise and experience, the best answers include multiple perspectives. Do you have relevant expertise or experience to contribute your answer to any of these commonly asked questions?
Add New Comment