I have a todo list in the database. Each Item has a todo item with a time by which it should be completed. What I want to do is have my C# application loop through the items and check if the item should’ve been completed an hour ago, at tops. Also the application must check if the item should be complete within the hour. If any of these two options is true, the job has to be shown to the user.
How can I make this work? I thought of the way below, but that will show any item that should’ve been completed an hour ago, or more. I want the items that should’ve been completed already, with an maximum of 1 hour ago.
foreach (DataRow dr in dsValue.Tables[0].Rows)
{
DateTime dt = Convert.ToDateTime(dr["tijd"]);
if (dt > DateTime.Now.AddHours(-1) || dt < DateTime.Now.AddHours(1))
{
MessageBox.Show(dr["opdracht"].ToString());
}
}
Your code would almost work — you want both conditions to be true, not either, so a quick change to:
…would suffice. It may be (much) more efficient to move the logic to the database, or at the very least precalculate “an hour ago” and “an hour’s time” outside of the loop.
For example, if your SQL looked a s little like