I have 3 text boxes that do a simple math equation, but I must be messing up my format. All Im doing is dividing the downpayment by the sales price. Then it should display the percentage of down payment in the downPaymentPercentTextBox. It works fine at first but when I clear out downPaymentTextbox I get this error:
ERROR MESSAGE WHEN DEBUGGING: Input string was not in a correct format.
Here is the code... Please tell me what Im doing wrong.. The method starts when I put a number in the downPaymentTextBox and the error happens when I manually delete the input from the same box... Im lost
private void downPaymentTextBox_TextChanged(object sender, EventArgs e)
{
decimal Price = Convert.ToDecimal(salesPriceTextBox.Text);
decimal DownPay = Convert.ToDecimal(downPaymentTextBox.Text);
decimal Percent = (DownPay / Price) * 100;
downPaymentPercentTextBox.Text = Convert.ToString(Percent);
}
ERROR MESSAGE WHEN DEBUGGING: Input string was not in a correct format.
Loading
Sam HobbsPosted Dec 3, 2010, 1:15 PM
Try putting the following in a console program:
You can create a console program that you can use to test code; I do.
Gaetano DPosted Dec 3, 2010, 1:03 PM
I know I could use a masked textbox, but I want to know how to do it through code..
Suthish NairPosted Dec 3, 2010, 1:03 PM
Sam HobbsPosted Dec 3, 2010, 1:02 PM
Gaetano DPosted Dec 3, 2010, 12:58 PM
I dont use commas. So thats not the problem... But thanks for the reply... Im still lost...
I know with decimal we are supposed to use the M, but Im not sure how to implement the M in my code... Any sugestions?
Sam HobbsPosted Dec 3, 2010, 12:54 PM
Try this. Don't use a comma; use "100000" instead of "100,000". If that works and you must use a comma in those values in the form, then you can add code that allows that.
Gaetano DPosted Dec 3, 2010, 12:32 PM
I replaced my code with yours and it apears to work, and then when I delete whats in the downPaymentTextBox I get the same error.
Input string was not in a correct format.
and in the troubleshooting tips I get: Make sure your method arguments are in the right format
Im really confused because I dont understand what the problem is.. this should be simple data type conversion from decimal to string... and it works then when I clear the textbox i get this error... If anybody can take a look and explain the problem I would greatly appreciate it.
* Also Im using big numbers in the text boxes.. I dont know if it matters. I put in 100,000 for the sales price and 20000 for the down payment. Once I delete the 20000, i get the error..
Suthish NairPosted Dec 3, 2010, 12:17 PM
Gomathi PalaniswamyPosted Dec 2, 2010, 11:59 PM
Your program has executed when am using like this
string a = "12",b="13";
decimal Price = Convert.ToDecimal(a);
decimal DownPay = Convert.ToDecimal(b);
decimal Percent = (DownPay / Price) * 100;
Console.WriteLine(Percent.ToString());
Console.ReadLine();
Sam HobbsPosted Dec 2, 2010, 8:11 PM