Problem with comparing DateTime
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Problem with comparing DateTime

  1. #1
    Join Date
    Apr 2012
    Posts
    2

    Problem with comparing DateTime

    Hi All!

    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.

    Thanks!

  2. #2
    Join Date
    Oct 2011
    Posts
    97

    Re: Problem with comparing DateTime

    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.

  3. #3
    Join Date
    Apr 2012
    Posts
    2

    Re: Problem with comparing DateTime

    Hi Access,

    I have tried using "=" instead of "==" but the problem persists with the same error...

  4. #4
    Join Date
    Oct 2011
    Posts
    97

    Re: Problem with comparing DateTime

    Well, I looked it up and I was definitely right about the = operator. As for the error, take a look as this page:

    http://msdn.microsoft.com/en-us/library/det4aw50.aspx

    In their expression they use the '#' symbol around the date. Maybe you could try that?

    Also, in your expression, what exactly is "Expiry date"? If that's a column name, I don't think that a column name can have a space in it.

  5. #5
    Join Date
    Jan 2010
    Posts
    1,099

    Re: Problem with comparing DateTime

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center