2015-07-12 72 views
-3

錯誤出現在此代碼中。很久以前,當我試圖解決小數和整數這個問題時,我得到了同樣的錯誤。我該如何解決這個問題?總計十進制小數時出錯

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 - 的購物SaveProducts =新購物車()

+1

你能提供的錯誤? – Padraic

+0

你收到了什麼錯誤?多一些信息,將不勝感激... – Saragis

+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如果解析失敗。 希望這有助於。

[編輯]

嘗試轉換string參數數類型時使用TryParse方法。它更安全得多。但是在執行進一步的代碼之前,您應該始終檢查返回值。如果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 

}; 
+0

謝謝先生。即時通訊有一個錯誤,說operator *不能被應用到decimal和double類型的操作數。這是在vat =(TotalPrice + ProductPrice)* 0.12; –

+0

嘗試使用0.12M而不是0.12。 我用這個值編輯我的代碼 – Umriyaev

+0

是的,刪除了錯誤。我再次嘗試,同樣的錯誤。嗯錯誤是在ShoppingCart k = new ShoppingCart()在我的btn_placeorder_click(第三代碼從我的問題)我真的很感謝你幫助我。 –