我一直收到mscorlib.dll中出现'System.InvalidCastException'类型的异常,但在RetailPrice的用户代码中没有处理

时间:2016-03-03 02:34:07

标签: c# .net visual-studio visual-studio-2012

protected void btnCalculateOrder_Click(object sender, EventArgs e)
{
    OrderSession();

    if (IsValid)
    {
        double RetailPrice = Convert.ToDouble(lblRetailPrice);
        double Quantity = Convert.ToDouble(txtQuantity);
        double Tax = (.9);

        double Subtotal = RetailPrice * Quantity;
        double TotalAmount = Subtotal + Tax;

        lblSubTotal.Text = Subtotal.ToString("c");
        lblTotalAmount.Text = TotalAmount.ToString("c");
    }

}

1 个答案:

答案 0 :(得分:4)

挑剔,你不需要括号:

double Tax = (.9); //change to double Tax = .9; or double Tax = 0.9;

问题:

好像你想要Convert Controls而不是Text的{​​{1}}

Controls

尝试将其更改为:

double RetailPrice = Convert.ToDouble(lblRetailPrice); //I assume this is a Label Control
double Quantity = Convert.ToDouble(txtQuantity); //and this is a TextBox Control