-2

This is the code containing the line that throws the exception:

weekhours = Convert.ToDouble(txtWeekHours.Text);
weekendhours = Convert.ToDouble(txtWeekendHours.Text); 
weekrate = Convert.ToDouble(txtWeekRate.Text);
weekendrate = Convert.ToDouble(txtWeekendRate.Text);

And this is the relevant part of the exception call stack:

System.FormatException was unhandled HResult=-2146233033 Message=Input string was not in a correct format.
at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Convert.ToDouble(String value)
at Wage_Dog.Form1.btnCalculate_Click(Object sender, EventArgs e) in C:\Users\Jerry\Desktop\MY C# APPS\Wage Dog Launch Edition\Wage Dog\Form1.cs:line 48
at System.Windows.Forms.Control.OnClick(EventArgs e)

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
Jach
  • 3
  • 5
  • 1
    show us some code. By this, there's error trying to convert string value to decimal.... at line 48 of your Form1 – Nino Feb 21 '17 at 22:41
  • What value are you attempting to "ParseDouble" on? If you provide some of your code, that should give people a better chance of helping. (edit: I was pipped to it by Nino!) – jonifen Feb 21 '17 at 22:41
  • weekhours = Convert.ToDouble(txtWeekHours.Text); weekendhours = Convert.ToDouble(txtWeekendHours.Text); weekrate = Convert.ToDouble(txtWeekRate.Text); weekendrate = Convert.ToDouble(txtWeekendRate.Text); – Jach Feb 21 '17 at 22:43
  • This is the lines where i get the error from – Jach Feb 21 '17 at 22:44
  • so, in your `txtWeekHours` or other text boxes there's something that cannot be converted to number (double, to be exact). It can be empty textbox, some letters, number with wrong decimal point (in example `1,5` instead of `1.5`)... – Nino Feb 21 '17 at 22:46
  • Okay, I get it when I don't fill the textbox fields with any values and I press the button to run the program. So how do I ensure that the program won't run if the user fails to put in some values in the textbox fields. From what you are saying it seems that is my problem exactly. Can you suggest some code that I can add to each of them to ensure that the user enters values before the program will run? – Jach Feb 21 '17 at 22:49
  • Either check each text value using `string.IsNullOrEmpty(txtWeekRate.Text)` or use validation in the form. – Mahendran Nadesan Feb 21 '17 at 22:52

2 Answers2

2

You need to validate user input. Conversion functions like ToDouble will fail when their argument is null, an empty string, or a string that does not represent a double in the current culture and number format.
There are many ways to achieve that:

  • use the built-in validation features of the Windows Forms framework
  • subclass textbox so it accepts only valid numbers. Reuse or make a control class for every kind of input (numeric, date/time etc.)
  • add code that parses the text and informs the user when something cannot be interpreted as a number. Wrap in a library if you need it on many forms.

For example:

try 
{
    weekhours = Convert.ToDouble(txtWeekHours.Text);
}
catch(FormatException)
{
    MessageBox.Show("Invalid input into the \"Week hours\" box. Enter a decimal number.");
}

There is also a TryParse function for every data type that supports Parse, which will return false instead of throwing an exception on text that fails to parse to the intended type.

Community
  • 1
  • 1
Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
0

on your button click, you can validate input like this:

double weekhours;
if (double.TryParse(txtWeekHours.Text, out weekhours)) 
{
    // do something with weekhours variable which now holds double(decimal) value
}
else
{
    //tell user he didn't enter valid number...
    // do something with weekhours variable which now holds double(decimal) value
}

double weekendhours;
if (double.TryParse(txtWeekendHours.Text, out weekendhours)) 
{
    // do something with weekendhours variable which now holds double(decimal) value
}
else
{
    //tell user he didn't enter valid number for weekendhours...
}

//etc...

So, with double.TryParse you're using method which converts the string representation of a number to its double-precision floating-point number equivalent. If that conversion succeeds, method returns true (and false if fails). That way you can control your code's flow, like in line if (double.TryParse(txtWeekHours.Text, out weekhours)). If conversion succeeded, weekhours variable will contain double (or decimal, to help you understand).

Nino
  • 6,931
  • 2
  • 27
  • 42
  • Personally, I think it would probably be better stylistically to reverse the order of the conditions (since that's basically a "double negative"). – EJoshuaS - Stand with Ukraine Feb 21 '17 at 23:27
  • Sorry if I sound like a dummy, but I am a new to C# and still finding my feet. Can you elaborate more on what you mean with an easy to understand example? I would appreciate it. – Jach Feb 21 '17 at 23:35
  • Thanks a lot, I just got my problem fixed with your code!! It works flawlessly now!! – Jach Feb 22 '17 at 03:11
  • @EJoshuaS you're right, that's a bit easier to understand, but i wrote this in notepad, late at night. Anyway, I'll edit my answer. Thans for suggestion. Jach, I thought code was self explanatory, and, as I said, i was late so i just wrote code without an explanation. Glad you understood it – Nino Feb 22 '17 at 07:41