Solved

CONTROLTOCOMPARE AND CONTROLTOVALIDATE in VB.NET

Posted on 2006-06-18
5
403 Views
Last Modified: 2008-03-10
in the compare validatore in VB.NET, there is CONTROLTOCOMPARE  and CONTROLTOVALIDATE.

can someone explain to e how we use this?

thanks
 
0
Comment
Question by:jskfan
  • 3
  • 2
5 Comments
 
LVL 19

Assisted Solution

by:arif_eqbal
arif_eqbal earned 250 total points
ID: 16931798
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
 

Author Comment

by:jskfan
ID: 16944368
I will test it and  get back with you later.
0
 

Author Comment

by:jskfan
ID: 16947537
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
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 250 total points
ID: 16948531
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
 
LVL 19

Assisted Solution

by:arif_eqbal
arif_eqbal earned 250 total points
ID: 16948680
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Gridview selected row 9 46
Get the selected ValueMember of Combobox 5 30
Set value of dynamically created checkboxlist 2 33
Error handling in asp.net site 5 26
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question