Pudge
Pudge

Reputation: 47

Input string was not in a correct format in a Formula

I was try to create a formula that will calculate the total amount and show it in a textbox , but there's an error that says " Input string was not in a correct format."

private void txtQUANTITYPOS_TextChanged(object sender, EventArgs e)
{
     txtTOTALPOS.Text = (Convert.ToDouble(txtPricePOS.Text) * Convert.ToDouble(txtQUANTITYPOS.Text)).ToString();
}

Can you help me please , thank you again in advance :)

Upvotes: 1

Views: 105

Answers (1)

ehh
ehh

Reputation: 3480

One possible scenario - You may have enter a number in your txtQUANTITYPOS textbox but txtPricePOS textbox is still empty. Therefore, you probably getting this error because your txtPricePOS textbox doesn't contains a number yet.

You could use Double.TryParse instead to ensure your are not multiplying other character rather than numbers:

Following an example of how you could implement it:

    private void txtQUANTITYPOS_TextChanged(object sender, EventArgs e)
    {
        double pricePos;
        double qtyPos;

        if (!double.TryParse(txtPricePOS.Text, out pricePos))
            return;  // Will return in case txtPricePOS.Text is not a number

        if (!double.TryParse(txtQUANTITYPOS.Text, out qtyPos))
            return;  // Will return in case txtPricePOS.Text is not a number

        txtTOTALPOS.Text = (pricePos * qtyPos).ToString();

    }

Upvotes: 2

Related Questions