Server Error in '/' Application.
The value '15/04/2024' of the MaximumValue property of 'RangeValidator2' cannot be converted to type 'Date'.
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.[HttpException (0x80004005): The value '15/04/2024' of the MaximumValue property of 'RangeValidator2' cannot be converted to type 'Date'.]
System.Web.UI.WebControls.RangeValidator.ValidateValues() +1482938
System.Web.UI.WebControls.RangeValidator.ControlPropertiesValid() +12
System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) +36
System.Web.UI.Control.PreRenderRecursiveInternal() +83
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Control.PreRenderRecursiveInternal() +155
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974
I have encountered this problem when try to use range validator control.
The reason is because the server date locale setting is "mm-dd-yyyy" (en-US) format. But I'm trying to assign "wrong" value "15/04/2004" (which in my application, i'm using dd/mm/yyyy format across all my date value) into the range validator maximum value.
To overcome this problem, these are what i do.
1. Rangevalidator control front end code
This is the culprit of the error. Make sure the cultureinvariantvalues is set to false.<asp:RangeValidator ID="RangeValidator2" runat="server" Enabled="false" EnableClientScript="true"
ErrorMessage="* selected date is over!"
ControlToValidate="TextBox1" SetFocusOnError="True" Text="* selected date is over!" ForeColor="#FF3300" CultureInvariantValues="false" Type="Date"></asp:RangeValidator>
2. Rangevalidator backend code
Ensure assigning the correct minimum & maximum date range at code behind.protected void Page_Load(object sender, EventArgs e)
{
RangeValidator2.MinimumValue = DateTime.Now.Date.ToShortDateString();
RangeValidator2.MaximumValue = DateTime.Now.Date.AddYears(10).ToShortDateString();
}
3. Web.config
This is to standardise our date comparison to culture="en-GB" which use "dd/mm/yyyy" independent of server localization setting. You may use the "en-US" and the format will be "mm-dd-yyyy".<globalization requestEncoding="UTF-8" responseEncoding="UTF-8" uiCulture="en-GB" culture="en-GB" />
No comments:
Post a Comment