如何在Windows窗体中将TextBox值从数据库转换为intger

时间:2015-12-08 11:10:54

标签: c# winforms

我正在使用以下代码..

        int TotalCost = 0;
        int Price = int.Parse(txtprice.Text);
        int Quantity = Convert.ToInt32(txtquantity.Text);
        int Discount = Convert.ToInt32(txtdiscount.Text);

        TotalCost += (Discount * Quantity);
        int NetCost = TotalCost * (1 - (Discount / 100));

        txtnetcost.Text = Convert.ToString(TotalCost);

txtprice.Text来自数据库。虽然我试图转换txtprice.Text它给出格式异常错误

1 个答案:

答案 0 :(得分:0)

您应该使用int.TryParse,即使exception不是要转换的value,它也不会给您valid int

int Price = 0;
int.TryParse(txtprice.Text.Trim(),out Price);

Trim textbox的值,因为extra spaces保存在database时可能包含java.net.URLConnetion.java