Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Selection of date from ms access database DateTime column shows "No Record Found".
The connection to datbase using "Microsoft.ACE.OLEDB.12.0" and the front end is VS C#

What I have tried:

connectionstring — ImgBB[^]
db — ImgBB[^]
form — ImgBB[^]
query — ImgBB[^]


C#
  con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\\xxxxxxx\\yyyyy\\zzzzz\\database.accdb;Persist Security Info=True;Jet OLEDB:Database Password=xyz";
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
        
 DateTime fromdate = Convert.ToDateTime(dtpfdt.Value.ToString("dd-MMM-yyyy") + " 00:00:00 AM");
                    DateTime todate = Convert.ToDateTime(dtptodt.Value.AddDays(1.0).ToString("dd-MMM-yyyy") + " 00:00:00 AM").AddSeconds(-1.0);
                string query = "SELECT * FROM [TRANSACTION] WHERE ([DT1] BETWEEN #" + fromdate + "# AND #" + todate + "#) OR ([DT2] BETWEEN #" + fromdate + "# AND #" + todate + "#)";
-----------;
----------;
--
.
.
.


dtpfdt==> Datetime Picker(From date)
dtpto==> Datetime Picker(To date)
[TRANSACTION]--- Table
[WT1]--- Column; Type--> Date/Time    Format--> General Date
[WT2]--- Column; Type--> Date/Time    Format--> General Date



THE DATATABLE
+---------------------+----------------------+
|      DT1            |         DT2          |
+--------------------------------------------+
| 24-04-20 1:38:35 AM | 25-04-20 1:41:35 AM  |
+---------------------+----------------------+
| 24-04-20 1:39:27 AM | 25-04-20 1:42:11 AM  |
+---------------------+----------------------+
| 25-04-20 1:40:22 AM | 25-04-20 1:42:39 AM  |
+---------------------+----------------------+



I have tried >= and <= condition instead of 'BETWEEN' and changed datetime format of datatime data column and date time picker and also system clock but nothing works.
Is anything wrong in my coding?
Kindly advise me to correct my mistakes.
Thanks.
Posted
Updated 23-Apr-20 20:05pm
v2

1 solution

SQL
string query = "SELECT * FROM [TRANSACTION] WHERE ([DT1] BETWEEN #" + fromdate + "# AND #" + todate + "#) OR ([DT2] BETWEEN #" + fromdate + "# AND #" + todate + "#)";

Such of query is SqlInjection[^] vulnerable!

Rather than it, use parameterized query:
SQL
string query = "SELECT * FROM [TRANSACTION] WHERE (([DT1] BETWEEN ? AND ?) OR ([DT2] BETWEEN ? AND ?));";


Usage:
C#
	string sConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=YourDatabase.accdb;Persist Security Info =False;";
	DataTable dt = new DataTable();

	DateTime fromdate = DateTimePickerDateFrom.Value;
	DateTime todate = DateTimePickerDateTo.Value;
	string sComm = "SELECT * FROM [TRANSACTION] WHERE (([DT1] BETWEEN ? AND ?) OR ([DT2] BETWEEN ? AND ?));";

	using (OleDbConnection oConn = new OleDbConnection(sConn))
		{
			oConn.Open();
			using (OleDbCommand oComm  = new OleDbCommand(sComm, oConn))
			{ //first part of Where condition
				oComm.Parameters.Add(new OleDbParameter(){Value=fromdate, OleDbType = OleDbType.DBTimeStamp ;});
				oComm.Parameters.Add(new OleDbParameter(){Value=todate, OleDbType = OleDbType.DBTimeStamp ;});
				//second part of Where condition
				oComm.Parameters.Add(new OleDbParameter(){Value=fromdate, OleDbType = OleDbType.DBTimeStamp ;});
				oComm.Parameters.Add(new OleDbParameter(){Value=todate, OleDbType = OleDbType.DBTimeStamp ;});				using (OleDbDataReader oRdr = oComm.ExecuteReader())
				{
					dt.Load(oRdr);
				}
			}
		}

if(dt.Rows.Count==0)
//no data!
else
//use can use data from dt ;)


Note: OleDb provider for MS Access database engine does not support named parameters! So, you have to add as many parameters as many is used in a query. The order of adding parameters to the SqlParametersCollection is very important!
 
Share this answer
 
v4
Comments
Member 11291660 24-Apr-20 3:43am    
Thanks Maciej Los,
I have tried your suggestion.
ExecuteReader()--> shows "Data type mismatch in criteria expression"
Maciej Los 24-Apr-20 4:24am    
Seems, your original table is using WT1 field name instead of DT1. So, change query accordingly.

Note: i've tested similar code on my database. And it's working fine!
Maciej Los 24-Apr-20 5:17am    
See updated answer!
Member 11291660 24-Apr-20 5:39am    
Mr. Maciej Los

Thanks a lot for your kindness. I really stuck for the last 4 days, regarding this issue. With the help of your kind guidance ,now my code is working flawlessly. I just did a little bit modification on the code that you suggested.


The working code is:
----------------------------------------------------------
DateTime fromdate = Convert.ToDateTime(dtpfdt.Value.ToString("dd-MMM-yyyy") + " 00:00:00 AM");
DateTime todate = Convert.ToDateTime(dtptodt.Value.AddDays(1.0).ToString("dd-MMM-yyyy") + " 00:00:00 AM").AddSeconds(-1.0);
query = "SELECT * FROM [TRANSACTION] WHERE (([DT1] BETWEEN @dtfrom AND @dtto) OR ([DT2] BETWEEN @dtfrom AND @dtto));";
OleDbConnection oConn = new OleDbConnection(con.ConnectionString);
oConn.Open();
OleDbCommand oComm = new OleDbCommand(query, oConn);
oComm.Parameters.Add(new OleDbParameter("@dtfrom",fromdate.ToString()));
oComm.Parameters.Add(new OleDbParameter("@dtto",todate.ToString()));
OleDbDataReader oRdr = oComm.ExecuteReader();
dt.Load(oRdr);
---------------------------------------------------------
Again Thanks.
Maciej Los 24-Apr-20 5:48am    
You should use using statement as i showed you! This is strongly recommended!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900