Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a grid view with a column showing date on which record was posted
I want the grid view to show data which is posted within last 15 days
How do I write my select command so that the only records less than 15 days old compared to current date and time are selected.

I am using ASP.NET with C#

I have tried something like "WHERE DatePosted <= DateTime.UtcNow" in the SqlSelectCommand
and also tried "WHERE DatePosted <= @datePosted" and added command.parameteres.AddwithValue("@DatePosted", DateTime.UtcNow) but apparently I am missing some important code, as it does not work

I am new to ASP.NET and desperately need help to deal with date column
Any kind of help that can be useful for a new person shall be highly appreciated

Kindly help
Best Regards
Posted
Updated 1-Nov-13 5:35am
v2

Try using the DATEADD() function. You can use negative numbers as well.

DATEADD() function tutorial[^]
 
Share this answer
 
A dynamic command can be given in codebehind. For benefit of those who are stuck at this problem like me, the syntax is
protected void nameofSqlDataSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["@DateSent"].Value = DateTime.UtcNow.AddDays(-15);
}
You can give any number for days in stead of 15. You can also define any date dynamically with reference to any control parameter.
 
Share this answer
 
v2

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