KFP
KFP

Reputation: 719

'input string not in a correct format' with a null value

I have a form with a button that adds the values of multiple labels(that are filled with currency values) and displays the sum in another label.

decimal[] totals = new decimal[11]; 

    private void calculate_Click(object sender, EventArgs e)
    {
        totals[0] = decimal.Parse(lblText1.Text, NumberStyles.Currency);
        totals[1] = decimal.Parse(lbltext2.Text, NumberStyles.Currency);
        lbltotal.Text = totals.Sum().ToString("C");
    }

Works fine until one is empty and I get 'input string not in a correct format'.

Upvotes: 0

Views: 317

Answers (1)

John Arlen
John Arlen

Reputation: 6689

  1. Don't bother calling if the text is empty
  2. Use decimal.TryParse

Upvotes: 1

Related Questions