Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that has 10 textboxes on it. 5 textboxes are auto populated from the database. the other 5 is for the user to enter numbers. I also have 5 rangevalidators on it. When I test the code I get an error. "Input string was not in a correct format. RangeValidatorLYTNFUG.MinimumValue = Convert.ToString(100 - Convert.ToInt32(TextBoxLYTNFUG.Text) * 20 / 100);" What did I do wrong? Can someone please help?

C#
protected void Page_Load(object sender, EventArgs e)
    {
        ButtonPrint.Attributes.Add("onclick", "window.print(); return false");

        RangeValidatorLYTNFUG.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNFUG.Text) - Convert.ToInt32(TextBoxLYTNFUG.Text) * 20 / 100);
        RangeValidatorLYTNFUG.MaximumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNFUG.Text) + Convert.ToInt32(TextBoxLYTNFUG.Text) * 20 / 100);

        RangeValidatorLYTNFG.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNFG.Text) - Convert.ToInt32(TextBoxLYTNFG.Text) * 20 / 100);
        RangeValidatorLYTNFG.MaximumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNFG.Text) + Convert.ToInt32(TextBoxLYTNFG.Text) * 20 / 100);

        RangeValidatorLYTNCPUG.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNCPUG.Text) - Convert.ToInt32(TextBoxLYTNCPUG.Text) * 20 / 100);
        RangeValidatorLYTNCPUG.MaximumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNCPUG.Text) + Convert.ToInt32(TextBoxLYTNCPUG.Text) * 20 / 100);

        RangeValidatorLYTNCPG.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNCPG.Text) - Convert.ToInt32(TextBoxLYTNCPG.Text) * 20 / 100);
        RangeValidatorLYTNCPG.MaximumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNCPG.Text) + Convert.ToInt32(TextBoxLYTNCPG.Text) * 20 / 100);

        RangeValidatorLYTNNCC.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNNCC.Text) - Convert.ToInt32(TextBoxLYTNNCC.Text) * 20 / 100);
        RangeValidatorLYTNNCC.MaximumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNNCC.Text) + Convert.ToInt32(TextBoxLYTNNCC.Text) * 20 / 100);
        TextBoxFTUG.Focus();


ASP.NET
<td>
                <asp:RangeValidator ID="RangeValidatorLYTNFUG" runat="server" 
                    ControlToValidate="TextBoxTNFUG" CssClass="style40" 
                    ErrorMessage="Number is Higher/Lower than 20%" ForeColor="Red" Type="Integer"></asp:RangeValidator>
            </td>
        </tr>
        <tr>
            <td class="style30">
                2. Total Number (headcount) Full-Time Post-Baccalaureate Students</td>
            <td class="style26">
                <asp:TextBox ID="TextBoxLYTNFG" runat="server" Width="180px" 
                    ReadOnly="True" Enabled="False"></asp:TextBox>
            </td>
            <td class="style25">
                <asp:TextBox ID="TextBoxTNFG" runat="server" Width="180px"></asp:TextBox>
            </td>
            <td>
                <asp:RangeValidator ID="RangeValidatorLYTNFG" runat="server" 
                    ControlToValidate="TextBoxTNFG" CssClass="style40" 
                    ErrorMessage="Number is Higher/Lower than 20%" ForeColor="Red" Type="Integer"></asp:RangeValidator>
            </td>
        </tr>
        <tr>
            <td class="style30">
                3. Total Number (headcount) For-Credit, Part-Time Undergraduate Students</td>
            <td class="style26">
                <asp:TextBox ID="TextBoxLYTNCPUG" runat="server" Width="180px" 
                    ReadOnly="True" Enabled="False"></asp:TextBox>
            </td>
            <td class="style25">
                <asp:TextBox ID="TextBoxTNCPUG" runat="server" Width="180px"></asp:TextBox>
            </td>
            <td>
                <asp:RangeValidator ID="RangeValidatorLYTNCPUG" runat="server" 
                    ControlToValidate="TextBoxTNCPUG" CssClass="style40" 
                    ErrorMessage="Number is Higher/Lower than 20%" ForeColor="Red" Type="Integer"></asp:RangeValidator>
            </td>
        </tr>
        <tr>
            <td class="style30">
                4. Total Number (headcount) For-Credit, Part-Time Post-Baccalaureate Students</td>
            <td class="style26">
                <asp:TextBox ID="TextBoxLYTNCPG" runat="server" Width="180px" 
                    ReadOnly="True" Enabled="False"></asp:TextBox>
            </td>
            <td class="style25">
                <asp:TextBox ID="TextBoxTNCPG" runat="server" Width="180px"></asp:TextBox>
            </td>
            <td>
                <asp:RangeValidator ID="RangeValidatorLYTNCPG" runat="server" 
                    ControlToValidate="TextBoxTNCPG" CssClass="style40" 
                    ErrorMessage="Number is Higher/Lower than 20%" ForeColor="Red" Type="Integer"></asp:RangeValidator>
            </td>
        </tr>
        <tr>
            <td class="style30">
                5. Total Number (headcount) students enrolled in Non-Credit courses</td>
            <td class="style26">
                <asp:TextBox ID="TextBoxLYTNNCC" runat="server" Width="180px" 
                    ReadOnly="True" Enabled="False"></asp:TextBox>
            </td>
            <td class="style25">
                <asp:TextBox ID="TextBoxTNNCC" runat="server" Width="180px" AutoPostBack="True" 
                    ontextchanged="TextBoxTNNCC_TextChanged"></asp:TextBox>
            </td>
            <td>
                <asp:RangeValidator ID="RangeValidatorLYTNNCC" runat="server" 
                    ControlToValidate="TextBoxTNNCC" CssClass="style40" 
                    ErrorMessage="Number is Higher/Lower than 20%" ForeColor="Red" Type="Integer"></asp:RangeValidator>
            </td>
        </tr>
        <tr>
            <td class="style30">
                6. Total Headcount of All Students</td>
            <td class="style26">
                 </td>
            <td class="style25">
                <asp:TextBox ID="TextBoxTHCAS" runat="server" Width="180px" Enabled="False" 
                    ReadOnly="True"></asp:TextBox>
            </td>
            <td>
                 </td>
        </tr>
    </table>
    <br />
Posted
Updated 15-Nov-13 4:27am
v5
Comments
♥…ЯҠ…♥ 15-Nov-13 8:15am    
Hey, the line should be like this
RangeValidator1.MaximumValue = Convert.ToString(Convert.ToInt32(TextBox1.Text) + Convert.ToInt32(TextBox1.Text) * 20 / 100);
RangeValidator1.MinimumValue = Convert.ToString(Convert.ToInt32(TextBox1.Text) - Convert.ToInt32(TextBox1.Text) * 20 / 100);
Computer Wiz99 15-Nov-13 8:16am    
Ok. So I don't have to set the control to validate?
♥…ЯҠ…♥ 15-Nov-13 8:25am    
You have to set control to validate.. the above code is like to compute only maximum and minimum range for the range validator... if textbox1 has DB value 100 then ==> (100 + (100 *(20/100))) ==> (100 + 20) ==> 120 (20% + dbvalue 100) and for minimum value its (100 - 20%) = 80. Now you got it? Change the rangeValidator type to integer range validator properties window
Computer Wiz99 15-Nov-13 8:27am    
Ok, so I don't have to set the range too, right?
♥…ЯҠ…♥ 15-Nov-13 8:28am    
no we are setting range at page load only, so need to set range at design time.....

1 solution

You need to post your ASP page, but I'm guessing you didn't properly identify the type of validation you want to do, here is an example (from MSDN):

ASP
<asp:rangevalidator id="Range1" xmlns:asp="#unknown">
           ControlToValidate="TextBox1"
           MinimumValue="1"
           MaximumValue="10"
           Type="Integer"
           EnableClientScript="false"
           Text="The value must be from 1 to 10!"
           runat="server"/>
</asp:rangevalidator>


Note the "Type" parameter which is set to "Integer", if you have that set incorrectly, you will get an exception.

Full MSDN Example[^]

[Edit]
To see what part is failing, try this as the first operation in your method:

C#
int textInt1 = Convert.ToInt32(TextBoxLYTNFUG.Text);
int textInt2 = Convert.ToInt32(TextBoxLYTNFUG.Text);

RangeValidatorLYTNFUG.MinimumValue = Convert.ToString(textInt1 - textInt2 * 20 / 100);


But its apparent from the above that the MinimumValue will always be zero (you are subtracting the same value from itself, so that equals zero, zero times anything is zero, and zero divided by 10 is zero).

Anyway, try the above and I think you may find that your text in the textbox isn't being converted properly.

[Edit 2]

Here is what you should do to calculate your min/max values:

C#
int value = ;

RangeValidatorLYTNFUG.MinimumValue = ((int)(Convert.ToInt32(TextBoxLYTNFUG.Text) * 0.8f)).ToString();
RangeValidatorLYTNFUG.MaximumValue = ((int)(Convert.ToInt32(TextBoxLYTNFUG.Text) * 1.2F)).ToString();


Which is much cleaner than what you wrote :)
 
Share this answer
 
v4
Comments
Computer Wiz99 15-Nov-13 10:28am    
Ron Beyer, Can you check my code and see where I went wrong?
Ron Beyer 15-Nov-13 10:34am    
You should break down your pieces and see what part is throwing the error, it may have nothing to do with the validator but an invalid format for the Convert.ToInt32, I will update my answer to show how to see this.
Computer Wiz99 15-Nov-13 10:36am    
The error says that the Input String was not in a correct format. Should I put the code after the auto populated textboxes?
Ron Beyer 15-Nov-13 10:39am    
I updated the solution, try adding that at the top of your function and see where the error lies. What is the text in the TextBoxLYTNFUG?
Computer Wiz99 15-Nov-13 10:41am    
They are all number in the database.

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