Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a web form that has 10 textboxes and 5 range validations. The first 5 textboxes have data that is populated from the database. The other 5 textboxes are the for input data. The range validator has a formula that will find out if the value is lower or higher than 20%. If the first populated textbox has a data of 651684 and the user enters 651684 the range validator does not fire because of this formula:
C#
int iMinVal = (itxtVal - itxtVal * 20 / 100);
.

Which means textbox1 - textbox2 * 20/100 = 0. So, 0 is my minimum value for this range.

When trying to set the maximum value I have this formula:
C#
int iMaxVal = (itxtVal + itxtVal * 20 / 100);
.

Which means textbox1 + textbox2 * 20/100 = 260673.6. So, 260273.6 is 20% higher than 651684. I am trying to make sure I have my calculation formula right to get the RangeValidation to fire correctly.

Here is the whole formula that I am using.

C#
int itxtVal = Convert.ToInt32(TextBox1.Text);
        int iMinVal = (itxtVal - itxtVal * 20 / 100);
        int iMaxVal = (itxtVal + itxtVal * 20 / 100);
        RangeValidator1.MinimumValue = iMinVal.ToString();
        RangeValidator1.MaximumValue = iMaxVal.ToString();


Is my calculation formula correct to finding out if the value in textbox2 is 20% higher or lower than the value in the populated textbox1?
Posted
Comments
CPallini 8-Sep-14 16:13pm    
How can
int iMinVal = (itxtVal - itxtVal * 20 / 100);
mean
textbox1 - textbox2 * 20/100
?
Computer Wiz99 8-Sep-14 16:16pm    
Because int itxtVal = Convert.ToInt32(TextBox1.Text); It is taking the value from textbox1 and ranging it to the value in textbox2. That is the way it is suppose to work. Is that correct?
CPallini 8-Sep-14 16:21pm    
Yes, but textbox2 has nothing to do with it, has it?
Computer Wiz99 8-Sep-14 16:26pm    
In a way it does. Textbox1 has the populated value. Textbox2 has the new entered value that the user enters. The formula calculates to see if the value in textbox2 is higher or lower than the value in textbox1 by 20%. I am just trying to make sure that my formula calculations is correct for the minimum value range and for the maximum value range.

1 solution

C#
int itxtVal = Convert.ToInt32(TextBox1.Text);
int iMinVal = (itxtVal - itxtVal * 20 / 100);
int iMaxVal = (itxtVal + itxtVal * 20 / 100);

So, if itxtVal = 100,
then iMinVal = 100 - 100 * 20 /100 = 100 - 20 = 80,
and iMaxVal = 100 + 100 * 20 / 100 = 120

So yes, your formula is correct.

UPDATE (from comments): Your formula can be greatly simplified making it easier to read and understand.
Also, using the Integer datatype causes your numbers to be rounded. If you don't want that, use Double instead. Don't forget to set the RangeValidator.Type property to Double as well.

C#
double txtVal = Convert.ToDouble(TextBox1.Text);
double minVal = (txtVal * 0.8);
double maxVal = (txtVal * 1.2);
RangeValidator1.MinimumValue = minVal.ToString();
RangeValidator1.MaximumValue = maxVal.ToString();
 
Share this answer
 
v2
Comments
Computer Wiz99 8-Sep-14 18:29pm    
kbrandwijk, so that if the value in textbox1 is 100 and the value in textbox2 is 120 then that will be a 20% increase right? Why is it not that I have it like this: int iMinVal = (itxtVal - itxtVal * .20 / 100);. Since it is 20%.
kbrandwijk 8-Sep-14 18:33pm    
Easier notation would be: minval = itxtVal * 0.8, maxval = itxtVal * 1.2. Because: x + (x / 20 * 100) = x + (x * .20) = 1.2x
Computer Wiz99 8-Sep-14 18:39pm    
Okay, so the way I am doing it is better for what I am doing? Or do I need to change it? What you put earlier, should I do them all that way? Is your way the correct way to do this or just leave it my way?
kbrandwijk 8-Sep-14 18:45pm    
The result is exactly the same. So you can leave it like it is, but seeing that you have trouble understanding the formula you are using, maybe itxtVal * 0.8 and itxtVal * 1.2 is easier for you to read and understand.
Computer Wiz99 8-Sep-14 18:47pm    
Okay. Can you do another solution that would replace what I have in my code?

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