Saturday, March 31, 2012

System.FormatException: Input string was not in a correct format....?

I've been working on this for a while, googled and everything, and i still can't work out what is wrong with this string... I keep getting this error:

System.FormatException: Input string was not in a correct format


public void btnCalculate_Click(object sender, System.EventArgs e)
{
double Label = Convert.ToDouble((Convert.ToInt32(lblQrtrs.Text)) + (Convert.ToInt32(lblOnes.Text)) + (Convert.ToInt32(lblTwos.Text)) + (Convert.ToInt32(lblFives.Text)) + (Convert.ToInt32(lblTens.Text)) + (Convert.ToInt32(lblTwenties.Text)) + (Convert.ToInt32(lblFifties.Text)) + (Convert.ToInt32(lblHundreds.Text)));
lblCalculate.Text = string.Format("{0:c}", Label);

Can anybody tell what i'm doing wrong here? Thanks,
Austin W.I should specify that each of those labels was already converted into a currency format from a textbox, like:


double Label = Convert.ToDouble(Convert.ToInt32(tbHundreds.Text) * 100);
lblHundreds.Text = string.Format("{0:c}", Label);

Just an update...

Ive converted my code to this...Its still not working but i feel like its on a better track than it was before, anybody got any ideas?


double Qrtrs = Convert.ToDouble(lblQrtrs.Text);
double Ones = Convert.ToDouble(lblOnes.Text);
double Twos = Convert.ToDouble(lblTwos.Text);
double Fives = Convert.ToDouble(lblFives.Text);
double Tens = Convert.ToDouble(lblTens.Text);
double Twenties = Convert.ToDouble(lblTwenties.Text);
double Fifties = Convert.ToDouble(lblFifties.Text);
double Hundreds = Convert.ToDouble(lblHundreds.Text);

double Label = (Qrtrs + Ones + Twos + Fives + Tens + Twenties + Fifties + Hundreds);
lblCalculate.Text = string.Format("{0:c}", Label);


Hi,
I am having the same problem, 'input string was not in a correctformat' when I try to convert something to double usingConvert.ToDouble(). I have a text box and if I enter something likethis, 123.45 and click on the submit button I get this error. Any help.
Thanks.


I think I got your answer. Now first I dontknow whats the string of your Label. Let's say that the label has"23.45" okay and you try to do the following:
string someString = Label1.Text;
int someInt = Int32.Parse(someString); // This will give error
It will give error since it was not able to parse the string as Int32. If you try to parse it as Double than it will run okay.
string someString = "23.45";
Double myDouble = Double.Parse(someString); // This will work fine.
Now if your lblhundred contains "100" without any decimal points than you can simply do this and it will work.
string someString = "455";
int someInt = Int32.Parse(someString);
Hope this will help!

Also check using the WebControl Type "Label" as a variable, like maybe change these two lines to:

double LabelValue = (Qrtrs + Ones + Twos + Fives + Tens + Twenties + Fifties + Hundreds);
lblCalculate.Text = string.Format("{0:c}", LabelValue);
NC...

0 comments:

Post a Comment