0

i have implemented a price text box wherein it shows 12,345 instead of 12345. Or sometimes. 12,345.00. However when being submitted to the database, it shows 12345. the comma is gone. any tricks on this? here is my place order button code:

 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 ? Convert.ToInt32(totalProducts) : 0,
                TotalPrice = totalPriceConversionResult ? Convert.ToInt32(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();
            }
            var customerId = Convert.ToInt32(dtResult.Rows[0][0]);
            Response.Redirect(String.Format("OrderSummary.aspx?Id={0}", customerId.ToString()));

Any tricks that i can do on the total price?

I tried using string.format to try if it will get 12,345 but i failed. here is the code:

lblTotalProducts.Text = Convert.ToString(dtCustomerDetails.Rows[0][String.Format("{0:#,000.00}","TotalProducts")]);

this line of code above will get data from the database.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Paolo Duhaylungsod
  • 487
  • 1
  • 7
  • 25

1 Answers1

0

In numeric data storage, the comma thousand separator is insignificant. It is only a presentation artifact. In your case, let the database store the value as 12345, but display it as 12,345.00 using string.Format method. Please see Standard Numeric Formats.

edit: lblTotalProducts.Text = String.Format("{0:#,000.00}",dtCustomerDetails.Rows[0]["TotalPr‌​oducts"]);

Null Void
  • 46
  • 4
  • i understand. but when i get the value from database, i want it to be 12,345 – Paolo Duhaylungsod May 21 '16 at 07:56
  • like for example here: lblTotalPrice.Text = Convert.ToString(dtCustomerDetails.Rows[0]["TotalPrice"]); this code gets the data from the database – Paolo Duhaylungsod May 21 '16 at 07:58
  • @PaoloDuhaylungsod So, it is on this line that you need to format it (using `string.Format`) to include the thousand separator. For example, `string.Format("{0:#,000.00}", 12345)`. – Null Void May 21 '16 at 08:02
  • whats wrong with this? lblTotalProducts.Text = Convert.ToString(dtCustomerDetails.Rows[0][String.Format("{0:#,000.00}","TotalProducts"]); error is ) expected – Paolo Duhaylungsod May 21 '16 at 08:08
  • (Now, we are getting in spood-feeding territory.) The error message says it, you have a misplaced `)`. Get the value separately as you would normally before calling `string.Format` on it. – Null Void May 21 '16 at 08:10
  • OK. Start at the beginning. Try `string.Format("{0:#,000.00}", 12345)` then work from there. – Null Void May 21 '16 at 08:18
  • please do see my edited question sir. i have lblTotalProducts.Text = Convert.ToString(dtCustomerDetails.Rows[0][String.Format("{0:#,000.00}","TotalProducts")]); – Paolo Duhaylungsod May 21 '16 at 08:19
  • `lblTotalProducts.Text = String.Format("{0:#,000.00}",dtCustomerDetails.Rows[0]["TotalPr‌​oducts"]); ` – Null Void May 21 '16 at 08:21
  • worked for me sir. thank you. post your answer so i can accept it. – Paolo Duhaylungsod May 21 '16 at 08:24