• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 420
  • Last Modified:

CONTROLTOCOMPARE AND CONTROLTOVALIDATE in VB.NET

in the compare validatore in VB.NET, there is CONTROLTOCOMPARE  and CONTROLTOVALIDATE.

can someone explain to e how we use this?

thanks
 
0
jskfan
Asked:
jskfan
  • 3
  • 2
3 Solutions
 
arif_eqbalCommented:
The Compare Validator is used when you want the value in a control to be in some way related to the value in another control, say you have two text boxes Txt1 and Tx2 you want that the value in Txt2 must be smaller than thatof Txt1 in that case you would use the Compare validator and here Control to Validate would be Txt2 and Control to Compare would be Txt1
For more info look at the examples at the site
http://www.w3schools.com/aspnet/control_comparevalidator.asp
0
 
jskfanAuthor Commented:
I will test it and  get back with you later.
0
 
jskfanAuthor Commented:
In The Compare Validator there is:

ControlToCompare
ControlToValidate
Operator
ValueToCompare

there is textbox1 and textbox2

if I want textbox2 to have a number of 9 or greater than 9
and the number entered in textbox2 to be greater than the number entered in textbox1.

how should I do that?

0
 
arif_eqbalCommented:
There are two validations that you require now
1. TextBox1 should have an Integer value Greater than or equal to 9
2. TextBox2 should have an Integer value greater than TextBox1 value

So we need 2 Validation controls
1. RangeValidator
    Put a Range Validator on your fomr and set the following properties
    ControlToValidate = TextBox1
    MinimumValue = 9
    MaximumValue = 65000 '(any max value that you want)
    Type = Integer

2. CompareValidator
    Put a CompareValidator and set the following properties
    ControlToValidate = TextBox2
    ControlToCompare = TextBox1
    Operator = "Greater Than"
    Type = Integer

Now test your App....
The properties are self explanatory though I'd just briefly describe what's being done
ControlToValidate is always the control that will be validated
ControlToCompare is the control whose value would be used for comparison and the comaprison could be "Equal To", "Greater Than" etc. and this is the Operator, lastly the Type tells us what data type we are trying to compare.
Property ValueToCompare is not required here, it is used to specify a static value to compare to, i.e. in case you wanted that instead of comparing the value with the TextBox1 value use a constant value you'd use that.
0
 
arif_eqbalCommented:
On the side Note, there are a few things, about validation controls, to remember
1. "No value" is a valid entry except in the case of RequiredFieldValidator
    >>   To test it, do not enter anything in the TextBox1 and click the PostBack button, it will not raise validation Error

2. If the value of the target control in the ControlToValidate property cannot be converted to the appropriate data type, validation Error is thrown
    >>  To test put a string or a char in textbox1 and check it will give validation error

3. If the value of the target control in the ControlToCompare property cannot be converted to the appropriate data type, No Error is thrown
    >> It means that if TextBox1 contained a string or a char and we said validate that TextBox2 has an Integer value greater than that in TextBox1 the result would always be "No Error" because the string in textbox1 can not be converted to Integer so no error is thrown
    To test this put a new TextBox say TextBox3, now change the CompareValidator's ControlToCompare property from textbox1 to TextBox3, Now if you put an integer in TextBox3 and test the TextBox2 value to be greater than this value it will validate properly, but if you enter a string in TextBox3 and validate it will never give Validation Error
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now