C#.Net: Custom Validator (ValueToCompare & ControlToValidate property errors)

Hi Everyone,

I’ve searched a fair bit round and tried a number of potential solutions, suggested on their respective threads on various sites, to which none have worked for me.

My objective is to make sure the date entered, and to be stored, is not ‘less than’ (before) the present date
i.e. Booking a reservation for a restaurant.

The first is:
“The ControlToValidate property of ‘cvDate’ cannot be blank…”

I’ve tried specifying it in page_load and .aspx source, the same has happened in both, in addition to ValueToCompare. I cant understand why it cant detect it?

C# (ControltoValidate):

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Globalization;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;

public partial class GenResEnquiry : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        IFormatProvider culture = new CultureInfo("en-GB", true);

        cvDate.ValueToCompare = DateTime.Today.ToShortDateString();
        cvDate.ControlToCompare = txtDate.Text.ToString();

NB: Page.Databind(); was one of those suggestions in another thread which hasnt yet hindered or helped it. others included, in the .aspx page source, changing ValueToCompare=<%= … to #


<asp:TextBox ID=“txtDate” runat=“server”></asp:TextBox> <asp:Label ID=“lblDateFormat” runat=“server” Text=“(dd/mm/yyyy)” />
<asp:CompareValidator ID=“cvDate” runat=“server” Operator=“LessThan” Display=“Dynamic” Type=“Date” ErrorMessage=“Invalid Reservation date.” />

I had also tried the following in the btnSubmit_Click to the error with proceeds it

            DateTime dateControl = Convert.ToDateTime(txtDate.Text);
            DateTime valueControl = Convert.ToDateTime(DateTime.Today.ToShortDateString());

            cvDate.ValueToCompare = valueControl.ToShortDateString();
            cvDate.ControlToCompare = dateControl.ToShortDateString();

“The value ‘’ of the ValueToCompare property of ‘cvDate’ cannot be converted to type ‘Date’.”

Any help is much appreiciated! :slight_smile:

Many Thanks,


You don’t need to use the .Text property here, or even codebehind at all. I’d use:

<asp:CompareValidator ID="cvDate" runat="server" Operator="LessThan" Display="Dynamic" Type="Date" ErrorMessage="Invalid Reservation date." ControlToCompare="txtDate" ValueToCompare="<# DateTime.Now.ToLongTimeString()>" /> 

That hasn’t worked either Im still getting the error:

"The ControlToValidate property of ‘cvDate’ cannot be blank.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The ControlToValidate property of ‘cvDate’ cannot be blank."

I’ve even tested it in a blank page just incase there is any conflictions elsewhere - same error. Is it worth posting the whole pages contents?

The script with what you suggested:


<asp:TextBox ID=“txtDate” runat=“server”></asp:TextBox> <asp:Label ID=“lblDateFormat” runat=“server” Text=“(dd/mm/yyyy)” />
<asp:CompareValidator ID=“cvDate” runat=“server” Operator=“LessThan” Display=“Dynamic” Type=“Date” ErrorMessage=“Invalid Reservation date.” ControlToCompare=“txtDate” ValueToCompare=“<# DateTime.Now.ToLongTimeString()>” />

Sorry, been off in MVC-land and haven’t used these for a while. Your ControlToValidate should be txtDate, ControlToCompare should not be used as you aren’t comparing against a control.

Ok, I’ve had another attempt at scripting it in the C# and its worked this time, without compare/custom validators

    protected void Button1_Click(object sender, EventArgs e)
        DataClassesBookingSysDataContext dbbs = new DataClassesBookingSysDataContext();
        GeneralEnqRes obj = new GeneralEnqRes();

        DateTime resDate = Convert.ToDateTime(txtDate.Text);
        DateTime todayDate = Convert.ToDateTime(DateTime.Today.ToShortDateString());

        if (cbReservation.Checked)
            if(txtDate.Text != "" || txtDate.Text != null)

                if (resDate < todayDate)
                    lblMessage.Text = "<font color='red'>Reservation date is before the current date!</font>";

                  //Add all fields to database

The script of a few examples one in particular a flight date checks pointed me in the right direction.

Thanks for your help!


Daniel :slight_smile:

You should convert to DateTime after checking that txtDate is null or empty (hint). And

DateTime todayDate = Convert.ToDateTime(DateTime.Today.ToShortDateString());

is useless. it should be

todayDate= DateTime.Today;