【问题标题】:System.FormatException: Input string was not in a correct format in C#System.FormatException:输入字符串在 C# 中的格式不正确
【发布时间】:2019-03-14 23:11:58
【问题描述】:

谁能帮我解决c#中的以下错误。

System.FormatException: Input string was not in a correct format

下面是代码:

CultureInfo culture = new CultureInfo("en-US");
txtTotalPrice.Text = totalPrice.ToString("c", culture);

private void btnCheck_Click(object sender, EventArgs e)
{
    // ...................................

    decimal totalPrice = Convert.ToDecimal(txtTotalPrice.Text.Split(',')[0]) * 1000;
    decimal finalPrice = totalPrice - (totalPrice / 100) * discount;

    // ......................................
}

enter image description here

【问题讨论】:

  • 点击按钮时txtTotalPrice.Text的内容是什么?
  • 哪一行抛出异常?
  • 检查decimal totalPrice = Convert.ToDecimal(txtTotalPrice.Text.Split(',')[0]) * 1000;这一行——文本框是问题的根源。您应该验证文本框的值以确保它是您认为的那样,而不是天真地将值输入转换函数。
  • txtTotalPrice.Text 在我们单击 btnCheck_Click 时使用。我不知道为什么当我更改如下代码时没有错误: CultureInfoculture = new CultureInfo("vi-VN"); txtTotalPrice.Text = totalPrice.ToString("c",culture); private void btnCheck_Click(object sender, EventArgs e) { // ................................... double totalPrice = Convert.ToDouble(txtTotalPrice.Text.Split(',')[0]) * 1000; double finalPrice = totalPrice - (totalPrice / 100) * 折扣; // ...................................... }

标签: c# string


【解决方案1】:

主要原因是,您转换为十进制的值是不可转换的。

请查看修改后的代码,如果适合您,请自行解决。

   decimal finalPrice;   
   decimal totalPrice;
    private void btnCheck_Click(object sender, EventArgs e)
    {
        // TO check if txtTotalPrice is not null
        if (!string.IsNullOrEmpty(txtTotalPrice.Text))
        {
            string decimalValue = txtTotalPrice.Text.Split(',')[0];
            decimal val;
            // Try to convert it to decimal.
            if (decimal.TryParse(decimalValue, out val))
            {
                // your logic here if able to convert it.
                finalPrice = val- (val/ 100) * discount;

            }
        }
   }

如果您有任何其他疑问,请告诉我。

【讨论】:

  • 哇!它对我有用。但是对于 finalPrice 获取值 0 为什么?帮我。 thnaks 代码如下:
  • CultureInfoculture = new CultureInfo("en-US");当我将代码更改为 CultureInfoculture = new CultureInfo("vi-VN");你的代码是工作。为什么?
  • 我只想使用“en-US”。帮我解决。谢谢。
  • 你能看到txtTotalPrice.Text中的值吗?
  • txtTotalPrice.Text = totalPrice.ToString("c");当我调试时,我发现它有价值。
【解决方案2】:

System.FormatException: 输入字符串格式不正确

错误

private void button3_Click_1(对象发送者,EventArgs e) {

        string UpdateQuery = "UPDATE useraccount SET Student_ID_No='"+textBox1.Text+"',FirstName='"+textBox2.Text+"',MiddleName='"+textBox3.Text+"',LastName='"+textBox4.Text+"',MobileNo='"+textBox5.Text+"',Position='"+comboBox1.Text+"',Password='"+textBox6.Text+"',Question='"+comboBox2.Text+"',Answer="+textBox7.Text+" WHERE id="+int.Parse(textBoxID.Text);
        executeMyQuery(UpdateQuery);
        populateDGV();

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 2011-11-23
    • 2022-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多