简体   繁体   中英

input string was not in correct format c# displaying a price on a label

Im new to c# and im trying to display a price of a product on a label, however when i run the page i get the error " input string was not in correct format" i have set the price as float.

this is the current code:

using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using WebApplication1.DataAccess;


namespace WebApplication1
{
public partial class Store : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GenerateControls();
    }

    private void GenerateControls()
    {
        ArrayList productsList = Connection.GetProductsByType("%");

        foreach (Products products in productsList)
        {
            Panel productPanel = new Panel();
            Image image = new Image { ImageUrl = products.Image, CssClass = " ProductsImage" };
            Literal literal = new Literal() { Text = "<br/>" };
            Literal literal2 = new Literal() { Text = "<br/>" };
            Label lblName = new Label { Text = products.Name, CssClass = "ProductsName" };
            Label lblPrice = new Label
           {
              Text = String.Format("{0.0.00}", products.Price + "<br/>"),
              CssClass = "ProductsPrice"
           };

            TextBox textBox = new TextBox
            {
                ID = products.Id.ToString(),
                CssClass = "ProductsTextBox",
                Width = 60,
                Text = "0"
            };

            RegularExpressionValidator regex = new RegularExpressionValidator
            {
                ValidationExpression = "^[0-9]*",
                ControlToValidate = textBox.ID,
                ErrorMessage = "Please enter number."
            };

            productPanel.Controls.Add(image);
            productPanel.Controls.Add(literal);
            productPanel.Controls.Add(lblName);
            productPanel.Controls.Add(literal2);
            productPanel.Controls.Add(lblPrice);
            productPanel.Controls.Add(textBox);
            productPanel.Controls.Add(regex);

            pnlProducts.Controls.Add(productPanel);

        }
    }
}

}

You get the error at

String.Format("{0.0.00}", products.Price + "<br/>")

That's not a valid format string, you either have to use:

String.Format("{0:0.00}", products.Price + "<br/>")

or

String.Format("{0}", products.Price.ToString("0.00") + "<br/>")

As an aside, you can put the <br/> also into the String.Format for better readability:

String.Format("{0:0.00}<br/>", products.Price)

I think you want something like this:

 Label lblPrice = new Label
 {
     Text = String.Format("{0:C}", products.Price + "<br/>"),
     CssClass = "ProductsPrice"
 };

Using {0:C) specifies that the string should be a Currency format.

See: String format currency

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM