Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi. I am from India (it may has something to do with culture info here). I am building a desktop application in C#.Net 2010 Express with MS-ACCESS 2010 32 bit as backend. I am using OLEDB for db connectivity.
I have a column named dt as Date/Time which has following values:
20-09-2016 22:53:32<br />
19-08-2016 22:54:24<br />
20-09-2016 22:56:01<br />
22-09-2016 22:56:27<br />
22-09-2016 22:56:41

I need to fetch the records By Date, By Month and By Year.
Till now I am not able to complete the Date part so couldnt work on month and year. Following is my code:
C#
b.com.CommandText = "SELECT * FROM srvtrans WHERE DateTime.Parse(dt)=@a ORDER BY sno DESC";
b.com.Parameters.Add("@a", dtp_srdmy.Value.ToShortDateString());
Show(dtp_srdmy.Value.ToShortDateString());
b.con.State == ConnectionState.Closed)
con.Close();
mytemp = new DataTable();
da.Fill(mytemp);

I have also tried following variations:
SQL
WHERE CONVERT(VARCHAR(10),dt,111)=@a

WHERE CONVERT(VARCHAR(10),dt,101)=@a

WHERE dt LIKE '%@a%'

WHERE DateTime.Parse(dt)=@a

WHERE dt=DateValue(@a)

WHERE CAST(dt AS DATE)=@a

WHERE CONVERT(varchar, dt, 101)=@a

WHERE DATE(dt)=@a

WHERE dt=@a

but none of them works for me. Please reply what updation should be made in the sql query to fetch records by date, by month and by year. Thanks in advance.

What I have tried:

I have also tried following variations:
SQL
WHERE CONVERT(VARCHAR(10),dt,111)=@a

WHERE CONVERT(VARCHAR(10),dt,101)=@a

WHERE dt LIKE '%@a%'

WHERE DateTime.Parse(dt)=@a

WHERE dt=DateValue(@a)

WHERE CAST(dt AS DATE)=@a

WHERE CONVERT(varchar, dt, 101)=@a

WHERE DATE(dt)=@a

WHERE dt=@a
Posted
Updated 22-Sep-16 9:20am
v3
Comments
[no name] 22-Sep-16 14:33pm    
"SELECT * FROM srvtrans WHERE DateTime.Parse(dt)=@a ORDER BY sno DESC"; not sure why you think this would work DateTime.Parse is .NET code not SQL. should be something like
"SELECT * FROM srvtrans WHERE dt=@a ORDER BY sno DESC";
Member 8057273 23-Sep-16 7:16am    
I have mentioned it in the question that I have already tried this variation

Try truncating the dates with a query like following
SQL
SELECT * FROM srvtrans WHERE DATEVALUE(dt)=DATEVALUE(@a) ORDER BY sno DESC

Just use a normal date/time as a parameter value. For more information have a look at DateValue Function - Access[^]
 
Share this answer
 
Comments
Member 8057273 23-Sep-16 0:55am    
This worked perfectly. Thanks a lot .
Wendelius 23-Sep-16 1:39am    
You're welcome :)
Member 8057273 23-Sep-16 7:11am    
Hey @Mike Wendelius. There is a problem. If I donot select a date in the datetimepicker control, it shows me error: "Data type mismatch in criteria expression." even though current date is visile in the control. I have triwd this on form load event: "dtp_srdmy.Value = System.DateTime.Now;" still i get the same error. If i select a date from the control (even if it is same as displayed), it works fine. I have used "MessageBox.Show(dtp_ardmy.Value.ToString());" to check the returned value and the value returned is same in both cases.
Wendelius 23-Sep-16 8:22am    
This sounds like you'd fetch the value only in click event (or similar). Instead you should get the value from the control when starting the query and prepare also for the possibility that there is no selection.

If you can't resolve the problem I'd suggest opening a new question and also posting appropriate code excerpts :)
Member 8057273 23-Sep-16 10:14am    
Yeah, I am getting the value of datetimepicker on button click event and passing it as a parameter to the sql query.
MS Access database engine doesn't know DateTime.Parse(dt)!

Change command properties to:
C#
b.com.CommandText = "SELECT * FROM srvtrans WHERE dt=@a ORDER BY sno DESC";
b.com.Parameters.Add("@a", dtp_srdmy.Value);

and everything should be OK.
 
Share this answer
 
Comments
Member 8057273 23-Sep-16 7:15am    
I have mentioned in the question that I have already tried this variation

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