I have DateTimePicker on the form where user selects expiry date for a task. Task and Expiry date is added to DataTable and displayed in DataGridView.
When I try to search for the tasks that end in i.e one day time I get an error:
"Syntax error: Missing operand after 'date' operator."
I am trying to do something like this:
Code:
DateTime day1 = DateTime.Now.AddDays(1);
string format = "dd/MM/yyyy HH:mm";
day1.ToString(format);
DataRow[] expiryRows = table.Select("Expiry date == day1");
foreach (DataRow row in expiryRows)
{
MessageBox.Show("task ends in one day");
}
I am new to programming, any help would be appreciated.
By the looks of it, it seems as if DataTable is somewhat similar to an SQL database. And judging by the error, my guess would be that you shouldn't be using '==' as the operator. That's only for languages where you can assign data using the '=' operator. In SQL (and I'm guessing this DataTable language), you just use the '=' operator for comparisons.
You were right to point the OP to that page: in the Remarks section, it says that the syntax used is described on the page for the Expression property of the DataColumn class.
So, it's not exactly SQL, but some DataTable filter expression language designed by MS.
The error is a bit misleading, I think, because, as far as I can tell, there is no "date operator" - it's just that the parser can't figure it out, and apparently assumes it is an operator of some kind...
What makes me say that is that I stumbled upon this forum where a similar question was asked, but the error message is: "Syntax error: Missing operand after '14' operator". Yeah...
Anyway, this is what it says there:
If a column name contains any non-alphanumeric characters or starts with a digit or matches (case-insensitively) any of the following reserved words, it requires special handling, as described in the following paragraphs.
[...]
[reserved words omitted...]
[...]
If a column name satisfies one of the above conditions, it must be wrapped in either square brackets or the "`" (grave accent) quotes. For example, to use a column named "Column#" in an expression, you would write either "[Column#]" or "`Column#`".
P.S. Also, as Access_Denied pointed out, read what it says about date literals.
Last edited by TheGreatCthulhu; April 23rd, 2012 at 06:52 PM.
Bookmarks