RangeValidator for DateTimes and SqlDateTimes in C#

Sponsored Links

73058_New Scooba® 230 Floor Washing Robot + Free Shipping!


RangeValidator for DateTimes and SqlDateTimes

On this page we'll look at how to use the ASP.NET RangeValidator, specifically for when dealing with DateTimes and SqlDateTimes using C#.

Let's say you're saving DateTimes in a SQL Server database. You have a TextBox to take the user input, you may have an AJAX or jQuery Calendar control to help users select a date easily and in a correct format. You've got a RequiredFieldValidator, maybe a RegularExpressionValidator or a CompareValidator to make sure the datetime is entered in the correct format. You could use the AJAX Extenders to limit user input to just entering numbers. So the user is pretty much guaranteed to enter a valid datetime. Right? Well, sometimes you know, expect the unexpected. Just when you think you've got the art of getting a DateTime to be a valid DateTime down to a fine grain, having several associated controls to make it very easy for the user to enter a valid DateTime, along comes a not very likely scenario, but one which can happen none the less, and bang you get an Exception.

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM

Now for a user to enter a DateTime before the days of year 1753, or after the year 9999, it's not very likely, but it can happen, but fear not, the RangeValidator is here to save the day! Here's an example of how to handle this error:

RangeValidator Example

Keeping things simple and to the point, we have a TextBox, a Button and a RangeValidator:

<asp:TextBox ID="TextBox_Date" runat="server" />
<asp:RangeValidator ID="RangeValidator_Date" runat="server" Type="Date" ControlToValidate="TextBox_Date" ErrorMessage="*" Display="Dynamic" />

<br />

<asp:Button ID="Button_SaveDate" Text="Save Date" runat="server" OnClick="Button_SaveDate_OnClick" />

The RangeValidator has had it's .Type property set to 'Date' so that it knows it's validating a DateTime value. We need to set the .MinimumValue and .MaximumValue properties as well. We could have done this declaratively like we had with the other properties by setting them in the .aspx page, but I wanted to show you something a little bit more special, so we'll need to set the .MinimumValue and .MaximumValue properties in the code behind.

SqlDateTime.MinValue and SqlDateTime.MaxValue

So as the error states: 'Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM'. SQL Server DateTimes have a lower range than their C# counterparts. We could have assigned these values declaratively on the ASPX page, but we're going to use the System.Data.SqlTypes struct to get values which have had these vales set in code for us to use, in public static readonly SqlDateTime objects. This allows to ensure we get the correct DateTime values to be used.

protected void Page_Load(object sender, EventArgs e)

    RangeValidator_Date.MinimumValue =     Convert.ToDateTime(System.Data.SqlTypes.SqlDateTime.MinValue.Value).ToShortDateString();
    RangeValidator_Date.MaximumValue =     Convert.ToDateTime(System.Data.SqlTypes.SqlDateTime.MaxValue.Value).ToShortDateString();

So now our RangeValidator will prevent the error shown above. Here's the code for the Button handler, just to remind you to call Page.Validate() and check Page.IsValid to ensure the server validation is called, should javascript be disabled (I've kept code to a minimum here for demo purposes, it's seriously lacking try catch error handling):

protected void Button_SaveDate_OnClick(object sender, EventArgs e)

    if (Page.IsValid)
        DataClassesDataContext db = new DataClassesDataContext();