Click here to Skip to main content
15,898,777 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a formula calculation that should find the 50% increase on a value. If a value is 100 and the 50% increase will be 151. I am trying to see if my code of getting this is correct. I tested this and the RangeValidator fires even when the number is the same or less than 50%. Please let me know if the code is correct or not.

C#
int itxtVal13 = Convert.ToInt32(TextBox1.Text);
        int iminVal13 = (itxtVal13 * 0);
        int imaxVal13 = (itxtVal13 + itxtVal13 * 150 / 100);
        RangeValidatorLYFTE4050.MinimumValue = iminVal13.ToString();
        RangeValidatorLYFTE4050.MaximumValue = imaxVal13.ToString();
Posted
Comments
Sergey Alexandrovich Kryukov 22-Oct-14 10:17am    
What, now arithmetic? :-)
—SA

1 solution

If you have itxtVal13 = 100 and put a breakpoint on the line
C#
RangeValidatorLYFTE4050.MinimumValue = iminVal13.ToString();

then examine the value of imaxVal13 what do you see?

You have calculated 250% of itxtVal13 not 150%

Either change the calculation to
int imaxVal13 = itxtVal13 + (itxtVal13 * 50 / 100);
OR to
int imaxVal13 =(itxtVal13 * 150 / 100);
See the difference?

[Edit]
The breakpoint on the line mentioned was just so that imaxVal13 had already been calculated. Your corrected code (this issue only) will look like:
C#
int itxtVal13 = Convert.ToInt32(TextBox1.Text);
int iminVal13 = (itxtVal13 * 0);  // Which = 0 by the way!!
int imaxVal13 = itxtVal13 * (150 / 100);
RangeValidatorLYFTE4050.MinimumValue = iminVal13.ToString();
RangeValidatorLYFTE4050.MaximumValue = imaxVal13.ToString();
 
Share this answer
 
v2
Comments
Computer Wiz99 22-Oct-14 10:38am    
CHill60, So in my code I am deleting this line, int iminVal13 = (itxtVal13 * 0);, and replacing this line, RangeValidatorLYFTE4050.MinimumValue = iminVal13.ToString();, with the line you gave me? RangeValidatorLYFTE4050.MinimumValue = iminVal13.ToString();
CHill60 22-Oct-14 10:43am    
That's not quite what I said - I've updated my solution
Computer Wiz99 22-Oct-14 11:35am    
That works. Thanks. Had to change some other ones when testing.
Computer Wiz99 22-Oct-14 11:40am    
Is there a way to have a validator to fire and give a value like 1 to be saved in the database? So that next time the user logs in and the form is populated the error will show and that user can print a report with that error on it?
CHill60 22-Oct-14 12:00pm    
Well that's not really the role of the validator - but there is nothing to stop you doing something like that in the code behind - don't get confused between a validation control acting on the client side (browser) and code being executed on the server

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900