I am wanting to pull only certain items from the database based on the current date and time. I have tried this, however it produces an error.
var student_Name = "";
var date = DateTime.Now;
var db = Database.Open("RESDatabaseSite");
var name = db.QueryValue("SELECT Student_Name FROM Student_Information WHERE Date_Enrolled <'@date'ORDER BY Date_Enrolled ASC");
Exception Details: System.Data.SqlServerCe.SqlCeException: The function is not recognized by SQL Server Compact. [ Name of function = Now,Data type (if known) = ]
You should probably mention what the original error was.
I see your query has a @date parameter in it, but your code doesn't show how that @date parameter gets its value set. I'm gonna assume that the error had something to do with that, and that you need to set a value for the @date parameter.
var db = Database.Open("RESDatabaseSite");
var name = db.QueryValue("SELECT Student_Name FROM Student_Information WHERE Date_Enrolled < DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))ORDER BY Date_Enrolled ASC");
Programming to simplify, dont look for hard way
Marked as answer by Thrasher114 on Dec 22, 2012 05:23 PM
Thrasher114
Member
19 Points
25 Posts
Using Current Date and Time to retrieve from a database
Dec 21, 2012 07:02 PM|LINK
I am wanting to pull only certain items from the database based on the current date and time. I have tried this, however it produces an error.
var student_Name = ""; var date = DateTime.Now; var db = Database.Open("RESDatabaseSite"); var name = db.QueryValue("SELECT Student_Name FROM Student_Information WHERE Date_Enrolled <'@date'ORDER BY Date_Enrolled ASC");johnyM456
Contributor
2177 Points
347 Posts
Re: Using Current Date and Time to retrieve from a database
Dec 21, 2012 07:35 PM|LINK
No need to use a variable. Try this:
var name = db.QueryValue("SELECT Student_Name FROM Student_Information WHERE Date_Enrolled < Now() ORDER BY Date_Enrolled ASC");johnyM456
Contributor
2177 Points
347 Posts
Re: Using Current Date and Time to retrieve from a database
Dec 21, 2012 07:40 PM|LINK
Or you could try and replace '@date'ORDER BY <--by--> @date ORDER BY (I dropped the brackets and inserted a space before ORDER)
var name = db.QueryValue("SELECT Student_Name FROM Student_Information WHERE Date_Enrolled < @date ORDER BY Date_Enrolled ASC");Thrasher114
Member
19 Points
25 Posts
Re: Using Current Date and Time to retrieve from a database
Dec 21, 2012 07:44 PM|LINK
The application returned this error
Exception Details: System.Data.SqlServerCe.SqlCeException: The function is not recognized by SQL Server Compact. [ Name of function = Now,Data type (if known) = ]
rossisdead2
Participant
1313 Points
300 Posts
Re: Using Current Date and Time to retrieve from a database
Dec 21, 2012 08:26 PM|LINK
You should probably mention what the original error was.
I see your query has a @date parameter in it, but your code doesn't show how that @date parameter gets its value set. I'm gonna assume that the error had something to do with that, and that you need to set a value for the @date parameter.
Kulrom
Contributor
4832 Points
892 Posts
Re: Using Current Date and Time to retrieve from a database
Dec 21, 2012 09:52 PM|LINK
My Blog: ASP.NET Stuff
oned_gk
All-Star
31257 Points
6389 Posts
Re: Using Current Date and Time to retrieve from a database
Dec 21, 2012 11:34 PM|LINK
var db = Database.Open("RESDatabaseSite"); var name = db.QueryValue("SELECT Student_Name FROM Student_Information WHERE Date_Enrolled < DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))ORDER BY Date_Enrolled ASC");Thrasher114
Member
19 Points
25 Posts
Re: Using Current Date and Time to retrieve from a database
Dec 22, 2012 05:28 PM|LINK
Thanks everyone for the help.