Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intermittent error with CompareValidator - ValueToCompare property is ""

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?

  1. I've checked the code - there is nowhere else that this is being set.
  2. Using the debugger, I've double checked at postback, and as expected, this property value is retained.
like image 826
Paddy Avatar asked Jul 19 '11 11:07

Paddy


3 Answers

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.

  1. Move the code of setting the ValueToCompare to the Init event, with removing the check for Postback.
  2. Check, what value has this property during data check (may be, you should remove check for Not IsPostBack).
  3. Set this property before check is occured.
like image 93
VMAtm Avatar answered Oct 13 '22 21:10

VMAtm


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".

like image 2
Samich Avatar answered Oct 13 '22 21:10

Samich


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.

like image 1
wonkim00 Avatar answered Oct 13 '22 21:10

wonkim00