错误在此代码中。很久以前,当我试图解决有关小数和整数的问题时,我得到了同样的错误。我怎么能解决这个问题?
protected void btnPlaceOrder_Click(object sender, EventArgs e)
{
string productids = string.Empty;
DataTable dt;
if (Session["MyCart"] != null)
{
dt = (DataTable)Session["MyCart"];
decimal totalPrice, totalProducts;
bool totalPriceConversionResult = decimal.TryParse(txtTotalPrice.Text, out totalPrice), totalProductsConversionResult = decimal.TryParse(txtTotalProducts.Text, out totalProducts);
ShoppingCart k = new ShoppingCart()
{
CustomerName = txtCustomerName.Text,
CustomerEmailID = txtCustomerEmailID.Text,
CustomerAddress = txtCustomerAddress.Text,
CustomerPhoneNo = txtCustomerPhoneNo.Text,
TotalProducts = totalProductsConversionResult ? totalProducts : 0,
TotalPrice = totalPriceConversionResult ? totalPrice : 0,
ProductList = productids,
PaymentMethod = rblPaymentMethod.SelectedItem.Text
};
DataTable dtResult = k.SaveCustomerDetails();
for (int i = 0; i < dt.Rows.Count; i++) // loop on how many products are added by the user
{
ShoppingCart SaveProducts = new ShoppingCart()
{
CustomerID = Convert.ToInt32(dtResult.Rows[0][0]),
ProductID = Convert.ToInt32(dt.Rows[i]["ProductID"]),
TotalProducts = Convert.ToInt32(dt.Rows[i]["ProductQuantity"]),
};
SaveProducts.SaveCustomerProducts();
}
Session.Clear();
GetMyCart();
lblTransactionNo.Text = "Your Transaction Number: " + dtResult.Rows[0][0];
pnlOrderPlaceSuccessfully.Visible = true;
pnlCheckOut.Visible = false;
pnlCategories.Visible = false;
pnlMyCart.Visible = false;
pnlEmptyCart.Visible = false;
pnlProducts.Visible = false;
SendOrderPlacedAlert(txtCustomerName.Text, txtCustomerEmailID.Text, Convert.ToString(dtResult.Rows[0][0]));
txtCustomerAddress.Text = string.Empty;
txtCustomerEmailID.Text = string.Empty;
txtCustomerName.Text = string.Empty;
txtCustomerPhoneNo.Text = string.Empty;
txtTotalPrice.Text = "0";
txtTotalProducts.Text = "0";
}
}
错误是位置0没有行。 - ShoppingCart SaveProducts = new ShoppingCart()
答案 0 :(得分:0)
试试这段代码:
private void UpdateTotalBill()
{
decimal vat = 0;
decimal TotalPrice = 0;
long TotalProducts = 0;
foreach (DataListItem item in dlCartProducts.Items)
{
Label PriceLabel = item.FindControl("lblPrice") as Label; // get price
TextBox ProductQuantity = item.FindControl("txtProductQuantity") as TextBox; // get quantity
Int64 priceLabel, productQuantity;
bool conversionResult = Int64.TryParse(PriceLabel.Text, out priceLabel);
if(!conversionResult) continue;
conversionResult = Int64.TryParse(ProductQuantity.Text, out productQuantity);
if(!conversionResult) continue;
decimal ProductPrice = priceLabel * productQuantity; //computation fro product price. price * quantity
vat = (TotalPrice + ProductPrice) * 0.12M; // computation for vat
vat = Math.Round(vat, 2);
TotalPrice = TotalPrice + ProductPrice;
TotalProducts = TotalProducts + productQuantity;
}
Label1.Text =Convert.ToString(vat);
txtTotalPrice.Text = Convert.ToString(TotalPrice + 40.0M + vat); // add vat + 40 delivery charge to total price
txtTotalProducts.Text = Convert.ToString(TotalProducts);
}
Int64结构具有TryParse方法,如果解析失败将返回false。 希望这会有所帮助。
[编辑]
将TryParse
参数转换为数字类型时,请尝试使用string
方法。它更安全。但是在执行更多代码之前,应始终检查返回值。如果TryParse
方法的返回值为false,则应停止执行代码并通知用户错误。
[编辑2]
更改此代码:
ShoppingCart k = new ShoppingCart()
{
CustomerName = txtCustomerName.Text,
CustomerEmailID = txtCustomerEmailID.Text,
CustomerAddress = txtCustomerAddress.Text,
CustomerPhoneNo = txtCustomerPhoneNo.Text,
TotalProducts = Convert.ToInt32(txtTotalProducts.Text),
TotalPrice = Convert.ToInt32(txtTotalPrice.Text),
ProductList = productids,
PaymentMethod = rblPaymentMethod.SelectedItem.Text
};
到以下代码:
decimal totalPrice, totalProducts;
bool totalPriceConversionResult = decimal.TryParse(txtTotalPrice.Text, out totalPrice), totalProductsConversionResult = decimal.TryParse(txtTotalProducts.Text, out totalProducts);
ShoppingCart k = new ShoppingCart()
{
CustomerName = txtCustomerName.Text,
CustomerEmailID = txtCustomerEmailID.Text,
CustomerAddress = txtCustomerAddress.Text,
CustomerPhoneNo = txtCustomerPhoneNo.Text,
TotalProducts = totalProductsConversionResult ? totalProducts : 0,
TotalPrice = totalPriceConversionResult ? totalPrice : 0,
ProductList = productids,
PaymentMethod = rblPaymentMethod.SelectedItem.Text
};