CompareValidator for dates > than today

Discussion in 'ASP .Net' started by tshad, Jan 13, 2006.

  1. tshad

    tshad Guest

    I am trying to check the date of my field to check if it is greater that
    today, but I am getting an error.

    I am using :

    <asp:CompareValidator runat="server"
    ControlToCompare="RequestedStartDate"
    Operator="GreaterThan"
    Display="Dynamic"
    ValueToCompare = DateTime.Today.ToShortDateString()
    Type="Date" text="<br>Invalid Start Date"/>

    But it gives me an error:

    The value 'DateTime.Today()' of the ValueToCompare property of '' cannot
    be converted to type 'Date'

    I also tried DateTime.Today(), but got the same error. How would I do this?

    Thanks,

    Tom
     
    tshad, Jan 13, 2006
    #1
    1. Advertising

  2. tshad

    tdavisjr Guest

    tshad wrote:
    > I am trying to check the date of my field to check if it is greater that
    > today, but I am getting an error.
    >
    > I am using :
    >
    > <asp:CompareValidator runat="server"
    > ControlToCompare="RequestedStartDate"
    > Operator="GreaterThan"
    > Display="Dynamic"
    > ValueToCompare = DateTime.Today.ToShortDateString()
    > Type="Date" text="<br>Invalid Start Date"/>
    >
    > But it gives me an error:
    >
    > The value 'DateTime.Today()' of the ValueToCompare property of '' cannot
    > be converted to type 'Date'
    >
    > I also tried DateTime.Today(), but got the same error. How would I do this?
    >
    > Thanks,
    >
    > Tom


    How about this:

    ValueToCompare = '<%= DateTime.Today.ToShortString() %>'

    put a semi-colon to the end of the ToShortString() if using C#. See if
    that works
     
    tdavisjr, Jan 13, 2006
    #2
    1. Advertising

  3. tshad

    tshad Guest

    "tdavisjr" <> wrote in message
    news:...
    >
    > tshad wrote:
    > > I am trying to check the date of my field to check if it is greater that
    > > today, but I am getting an error.
    > >
    > > I am using :
    > >
    > > <asp:CompareValidator runat="server"
    > > ControlToCompare="RequestedStartDate"
    > > Operator="GreaterThan"
    > > Display="Dynamic"
    > > ValueToCompare = DateTime.Today.ToShortDateString()
    > > Type="Date" text="<br>Invalid Start Date"/>
    > >
    > > But it gives me an error:
    > >
    > > The value 'DateTime.Today()' of the ValueToCompare property of ''

    cannot
    > > be converted to type 'Date'
    > >
    > > I also tried DateTime.Today(), but got the same error. How would I do

    this?
    > >
    > > Thanks,
    > >
    > > Tom

    >
    > How about this:
    >
    > ValueToCompare = '<%= DateTime.Today.ToShortString() %>'
    >
    > put a semi-colon to the end of the ToShortString() if using C#. See if
    > that works
    >

    That was what I was looking for.

    Thanks,

    Tom
     
    tshad, Jan 17, 2006
    #3
  4. tshad

    tmartin40

    Joined:
    Apr 15, 2007
    Messages:
    1
    Dates less than current date

    I'm using:

    <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox7"
    Display="Dynamic" ErrorMessage="CompareValidator" Operator="LessThan" Type="Date"
    ValueToCompare = '<%= DateTime.Today.ToShortString() %>'></asp:CompareValidator>

    I am getting:

    The value '<%= DateTime.Today.ToShortString() %>' of the ValueToCompare property of 'CompareValidator1' cannot be converted to type 'Date'.

    Can someone please help me?
     
    tmartin40, Apr 16, 2007
    #4
  5. tshad

    natty_dread

    Joined:
    Aug 7, 2007
    Messages:
    1
    Did you try to replace where you had:

    ValueToCompare = '<%= DateTime.Today.ToShortString() %>'

    With

    ValueToCompare="<%# DateTime.Today.ToShortDateString() %>"


    This worked for me

    PS - You also need to call Page.DataBind() in Page_Load for this syntax to work
     
    Last edited: Aug 7, 2007
    natty_dread, Aug 7, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jorge EA
    Replies:
    1
    Views:
    584
    Peter Blum
    Sep 4, 2004
  2. gauthier
    Replies:
    0
    Views:
    662
    gauthier
    Jun 30, 2005
  3. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    911
    Rob Schieber
    Sep 30, 2005
  4. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    235
    Mark Schupp
    Aug 9, 2004
  5. Replies:
    1
    Views:
    240
    Jano Svitok
    Jul 17, 2007
Loading...

Share This Page