Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有十进制总价时出错_C#_Decimal - Fatal编程技术网

C# 具有十进制总价时出错

C# 具有十进制总价时出错,c#,decimal,C#,Decimal,错误在这段代码中。很久以前,当我试图解决有关小数和整数的问题时,我遇到了相同的错误。我如何解决这个问题 protected void btnPlaceOrder_Click(object sender, EventArgs e) { string productids = string.Empty; DataTable dt; if (Session["MyCart"] != null) { dt =

错误在这段代码中。很久以前,当我试图解决有关小数和整数的问题时,我遇到了相同的错误。我如何解决这个问题

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=新建ShoppingCart

请尝试以下代码:

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);
}
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

};
Int64结构具有TryParse方法,如果解析失败,该方法将返回false。 希望这有帮助

[编辑]

将字符串参数转换为数字类型时,请尝试使用TryParse方法。这样更安全。但是在执行进一步的代码之前,您应该始终检查返回值。如果TryParse方法的返回值为false,则应停止执行代码并将错误通知用户

[编辑2]

更改此代码:

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);
}
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

};

你能提供错误吗?你收到了什么错误?如果您能提供更多的信息,我将不胜感激。另外请注意,您所说的是十进制,但就我所知,您的代码只使用了double。十进制更合适。@Jon Skeet是的,我知道,先生:但你能告诉我我要更改哪些特定代码或什么吗?谢谢你。确实需要这样做才能让它工作:不过,从根本上说,你应该学会如何自己诊断这个问题——准确地找出问题所在,你发布的大部分代码都变得无关紧要。在发布问题之前,您对问题的诊断进展如何?堆栈溢出并不意味着要取代为自己解决问题所做的艰苦工作……谢谢您,先生。我有一个错误,说运算符*不能应用于decimal和double类型的操作数。增值税=总价+产品价格*0.12;尝试使用0.12米而不是0.12米。我用这个值编辑了代码,删除了错误。我又试了一次,同样的错误。嗯,错误在我的btn中的ShoppingCart k=新ShoppingCart\u placeorder\u单击我问题中的第三个代码我真的很想感谢你帮助我。我尝试将TotalPrice=Convert.ToInt32txtTotalPrice.Text更改为TotalPrice=Convert.ToDecimalTtotalPrice.Text,但给我一个错误,无法将十进制转换为整数:程序推送通过,但总价格结果为0,先生: