Click here to Skip to main content
15,914,250 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that has 5 rangevalidators on it. When the page loads this error comes up:

HTML
Input string was not in a correct format.
Line 82:         RangeValidatorLYTNFUG.MinimumValue = Convert.ToString(Convert.ToInt32(TextBoxLYTNFUG.Text) - Convert.ToInt32(TextBoxLYTNFUG.Text) * 20 / 100);


So I made sure that the database the textboxes was populating from had data in it which it does. I have these codes in place for all 5 rangevalidators:

C#
protected void Page_Load(object sender, EventArgs e)
{
 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);
}


The code works but I also add another textbox populated code to populate the other textboxes on the form of the data that the user just submitted. There is nothing in that database yet until the user submits it. What am I doing wrong to get this error?

Here is the TextBox_TextChange Code:

C#
protected void TextBoxTNNCC_TextChanged(object sender, EventArgs e)
    {
        int a = Convert.ToInt32(TextBoxTNFUG.Text);
        int b = Convert.ToInt32(TextBoxTNFG.Text);
        int c = Convert.ToInt32(TextBoxTNCPUG.Text);
        int d = Convert.ToInt32(TextBoxTNCPG.Text);
        int f = Convert.ToInt32(TextBoxTNNCC.Text);
        TextBoxTHCAS.Text = Convert.ToString(a + b + c + d + f);

    }
Posted
Updated 4-Dec-13 7:37am
v2

1 solution

This error occurs because of conversion. The value is non-numeric or blank string.
Check this similar question with answer to solve your issue.
Conversion from string to int[^]

Few suggestions:
Don't put massive code in Page_Load(), create a function/sub procedure for that & call it in Page_Load() like below.
C#
protected void Page_Load(object sender, EventArgs e)
{
    InitRangeValidations();
}
protected void InitRangeValidations()
{
 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);

/*
Remaining massive code blah blah blah
*/
}

And try to simplify your code, the below one is simpler than yours, agree? It's more readable one. I could simplify even more than this(like reducing 2 or 3 lines).
C#
protected void InitRangeValidations()
{
int itxtVal = Convert.ToInt32(TextBoxLYTNFUG.Text);//Modify this line based on above link as I'm giving you as exercise for you
int iMinVal = (itxtVal - itxtVal * 20 / 100);
int iMaxVal = (itxtVal + itxtVal * 20 / 100);
RangeValidatorLYTNFUG.MinimumValue = iMinVal.ToString();
RangeValidatorLYTNFUG.MaximumValue = iMaxVal.ToString();
}

I know you're posting questions here with clear details regularly, keep doing this. Here suggestions for you to optimize your code.
Some practices to write better C#/.NET code[^]
Some Best Practices for C# Application Development (Explained)[^]
 
Share this answer
 
Comments
Computer Wiz99 4-Dec-13 10:57am    
thatraja, Thanks for the info, code and links!!! I will do this. Plus, any reason why the rangevalidator stops firing when the Textbox_TextChange is happening on the last textbox? And where you have (itxtVal- itxtVal * 20/100);, I change that to the ID of my textbox right?
thatraja 4-Dec-13 12:31pm    
/*any reason why the rangevalidator stops firing when the Textbox_TextChange is happening on the last textbox?*/
You didn't share those code, include that in your question.
/*And where you have (itxtVal- itxtVal * 20/100);, I change that to the ID of my textbox right?*/
Check the previous line(I have declared variable & assigned value with what?). Read the answer fully & slowly. Focus, concentrate.......
Computer Wiz99 4-Dec-13 13:37pm    
Yes My Master. LoL!! Ok, sorry for the long wait. I have uploaded the code for the TextBox_TextChange that has the rangevalidators on it.
Computer Wiz99 4-Dec-13 14:33pm    
Ok. I have rewrote the code that you gave me and when the page loads I get this error: Input string was not in a correct format. int itxtVal = Convert.ToInt32(TextBoxLYTNFUG.Text);. Now what did I do wrong?

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