I have a page with a CompareValidator on it:
<asp:textbox id="txtResponseDate" runat="server" />
<asp:requiredfieldvalidator id="rfvResponseDate" runat="server"
controltovalidate="txtResponseDate"
display="Dynamic"
errormessage="Date is required."
setfocusonerror="true">
</asp:requiredfieldvalidator>
<asp:comparevalidator id="cmvDate" runat="server"
controltovalidate="txtResponseDate"
display="Dynamic"
errormessage="Date must not be before today."
operator="GreaterThanEqual"
setfocusonerror="true"
type="Date">
</asp:comparevalidator>
In the code behind, we set the ValueToCompare property like so:
If Not IsPostBack Then
cmvDate.ValueToCompare = DateTime.Now.ToString("d")
End If
Intermittently (we can't discern a pattern), we get the following error:
"The value '' of the ValueToCompare property of 'cmvDate' cannot be converted to type 'Date'."
With a call stack of:
at System.Web.UI.WebControls.CompareValidator.ControlPropertiesValid()
at System.Web.UI.WebControls.BaseValidator.get_PropertiesValid()
at System.Web.UI.WebControls.BaseValidator.Validate()
at System.Web.UI.Page.Validate()
at System.Web.UI.Page.Validate(String validationGroup)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
My first thought was something odd with the date format, but we're using UK dates, last error was on 18th July, so the ValueToCompare gets set to 18/07/2011. I've set this manually using the debugger, and it worked fine. Anybody any bright ideas as to why this may be happening?
The value '' of the ValueToCompare property of 'cmvDate' cannot be converted to type 'Date'.
This error message says, that in moment of validation property ValueToCompare
of your rangeValidator itself (not the control you are validating) is not set. This can be if you wrote this lines:
If Not IsPostBack Then
cmvDate.ValueToCompare = DateTime.Now.ToString("d")
End If
not in Page_Init
event.
ValueToCompare
to the Init event, with removing the check for Postback.Not IsPostBack
).In your control settings you set type="Date"
whitch means that you will compare value as a DateTime
object, but in the ValueToCompare
property you set string
object, not a DateTime
.
If you need to compare by strings - set control setting type
to String
,
If you need to compare by dates - set DateTime
object to the ValueToCompare
control property and Operator="DataTypeCheck"
.
Are you getting any other ViewState-related errors? Like "Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey>
configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster."?
If so, maybe the problem is related to one of the things listed in this MS KB article: Intermittent Invalid Viewstate Error in ASP.NET Web pages.
There are several conditions that may cause this issue. Each known condition is presented with a short explanation and a possible workaround.
Application Pool Recycling
Server Farms or Server Clusters
Form Posts
Proxy servers and Virus Scanners
One other possible thing not mentioned in that article is a slow or unreliable connection along with resulting impatience of users. I worked on an app hosted in the US but many users are from India. We noticed a much greater frequency of these types of intermittent ViewState-related errors among the users in India. We speculated that it might have been because of higher latency, greater number of hops, lower bandwidth and users interacting with pages prior to page load completion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With