0

The problem of my code is if I put value in pay money(textbox) and I turned to zero giving me an error

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

This is my code :

private void txtPM_TextChanged(object sender, EventArgs e)
{
    string fee = lblFee.Text.Trim();
    string pm = txtPM.Text.Trim();
    int number = Convert.ToInt32(fee);
    int number2 = Convert.ToInt32(pm);

    int minus = number2 - number;
    txtChange.Text = minus.ToString().Trim();
}

I will put here the form

enter image description here

I hope you can help me thanks guys

MatSnow
  • 7,357
  • 3
  • 19
  • 31
  • 3
    Check whether the strings are null or empty before calling ToString(), Trim() and Convert.ToInt32() as well. – Raviraj Palvankar May 03 '18 at 05:57
  • 3
    Possible duplicate of [Input string was not in a correct format](https://stackoverflow.com/questions/8321514/input-string-was-not-in-a-correct-format) – MatSnow May 03 '18 at 06:08

3 Answers3

0

You may need Convert.ToDecimal() if you are working with monetary values specified as decimals. (I second the empty and null checking suggestions)

Update for clarification:

Currently, you are converting string to Int32 with the following:

int number = Convert.ToInt32(fee);
int number2 = Convert.ToInt32(pm);

You can instead convert to decimal with the following:

decimal number = Convert.ToDecimal(fee);
decimal number2 = Convert.ToDecimal(pm);

Update2 (full method updated with null and empty checks):

private void txtPM_TextChanged(object sender, EventArgs e)
{
    string fee = lblFee.Text.Trim();
    string pm = txtPM.Text.Trim();

    decimal number = 0;
    decimal number2 = 0;

    if(!string.IsNullOrWhiteSpace(fee)) number = Convert.ToDecimal(fee);
    if(!string.IsNullOrWhiteSpace(pm)) number2 = Convert.ToDecimal(pm);

    decimal minus = number2 - number;
    txtChange.Text = minus.ToString().Trim();
}
egnomerator
  • 985
  • 8
  • 15
  • how sir? the variable i use in beginning is string, cannot convert to decimal. – Francis Daniel V. Morales May 03 '18 at 06:12
  • @FrancisDanielV.Morales and why not? What's the error? It's working for me. Also, you already use convert.toin32 so why would todecimal not work?? – slow May 03 '18 at 06:13
  • @FrancisDanielV.Morales I've updated the answer to clarify. – egnomerator May 03 '18 at 06:18
  • its says that cannot implicitly convert type 'decimal' to int. an explicit conversion exists – Francis Daniel V. Morales May 03 '18 at 06:19
  • i try the updated code but if I turned the value to zero its giving me the same error sir string fee = lblFee.Text.Trim(); string pm = txtPM.Text.Trim(); decimal number = Convert.ToDecimal(fee); decimal number2 = Convert.ToDecimal(pm); decimal minus = number2 - number; txtChange.Text = minus.ToString().Trim(); – Francis Daniel V. Morales May 03 '18 at 06:22
  • @egnomerator your answer doesn't solve the exception, the exceptions occur because `fee` and `pm` are not in the right format. The strings fee and pm must contain only digits for the conversion to be successful. – Slaven Tojić May 03 '18 at 06:30
  • @SlavenTojić my only problem is if i put a value in paymoney (textbox) and if I erased it give me a error. can u help me? – Francis Daniel V. Morales May 03 '18 at 06:35
  • @FrancisDanielV.Morales hopefully this last update takes care of it. I included how to check for null and empty before attempting to convert. – egnomerator May 03 '18 at 06:35
  • @FrancisDanielV.Morales I solved your problem with `int.TryParse` Take a look. – Slaven Tojić May 03 '18 at 06:42
  • 2
    @FrancisDanielV.Morales I thinks its better to use TryParse then this approach with less code you get the same result, but it's on you to decide. – Slaven Tojić May 03 '18 at 06:51
  • 1
    @SlavenTojić I agree that the TryParse approach and more specifically the answer by MatSnow shows a nice way to refactor the solution – egnomerator May 03 '18 at 07:01
  • 1
    @egnomerator MatSnows solution si more compact then mine but i wanted to keep it simple for the OP and dont change to much only fix the exception. And BTW MatSnows solution only works with C#7 and I don't know if the OP's complier uses C#7 – Slaven Tojić May 03 '18 at 07:09
  • 1
    @SlavenTojić Oh that's an interesting point, I didn't know it was C#7 specific. Nice point. And, I had a similar goal to yours with my suggested solution - TryParse was my initial thought, but I went with Convert.ToDecimal with the intention of staying close to the OP's approach. – egnomerator May 03 '18 at 07:19
0

Use the decimal type because it's more appropriate for financial and monetary calculations. To avoid the exception use decimal.TryParse. If the strings pm and fee are not in the right format decimal.TryParse will return zero and no exception will be thrown. You can also remove the Trim() on the last line.

private void txtPM_TextChanged(object sender, EventArgs e)
{
    string fee = lblFee.Text.Trim();
    string pm = txtPM.Text.Trim();
    decimal number;
    decimal.TryParse(fee, out number);
    decimal number2;
    decimal.TryParse(pm, out number2);

    decimal minus = number2 - number;
    txtChange.Text = minus.ToString();
}
Slaven Tojić
  • 2,945
  • 2
  • 14
  • 33
0

You can use TryParse as already pointed out in the duplicate.
Also it makes more sense to use decimal when working with monetary values.

private void txtPM_TextChanged(object sender, EventArgs e)
{
    decimal.TryParse(lblFee.Text, out decimal number);
    decimal.TryParse(txtPM.Text, out decimal number2);

    txtChange.Text = (number2 - number).ToString();
}

When using C# 6.0 or earlier you have to declare the variables in a separate statement:

private void txtPM_TextChanged(object sender, EventArgs e)
{
    decimal number;
    decimal.TryParse(lblFee.Text, out number);
    decimal number2;
    decimal.TryParse(txtPM.Text, out number2);

    txtChange.Text = (number2 - number).ToString();
}
MatSnow
  • 7,357
  • 3
  • 19
  • 31