【问题标题】:exception( Input string wasn't in the correct format )异常(输入字符串格式不正确)
【发布时间】:2013-12-04 19:45:58
【问题描述】:

我执行这段代码:

  t1.Rows[gid].Cells[3].Paragraphs.First()
                              .Append((Int32.Parse(calculateGetunitPrice(tblMaterialGroup.id))*(a+b+c)).ToString())
                              .FontSize(12)
                              .Font(new FontFamily("B Nazanin"));

a、b、c的值为:

a=24121
b=0
c=13311

并且“calculateGetunitPrice(tblMaterialGroup.id)”为 1

我收到了这个错误:

Input string wasn't in the correct format

这是我的 calculateGetunitPrice 函数:

public string calculateGetunitPriceForElse(int materialGroupId)
    {

        var q = from i in dbconnect.tblMaterialTenderAnnouncePrices
                where i.companyId == _comWinnerID && i.MaterialGroupId == materialGroupId
                select i.PriceForElse;

       // int? sum = g => g.PriceForElse;
        return q.ToString();


    }

【问题讨论】:

  • 显然calculateGetunitPrice(tblMaterialGroup.id))*(a+b+c)).ToString() 的格式不正确。检查此表达式的调试器值。
  • 你能发布你的整个调用堆栈吗?
  • 那我该怎么做呢?
  • 在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean
  • q 是集合所以使用q.First().ToString() 或类似的东西

标签: c# winforms linq datagrid


【解决方案1】:

在这段代码中

var q = from i in dbconnect.tblMaterialTenderAnnouncePrices
        where i.companyId == _comWinnerID && i.MaterialGroupId == materialGroupId
        select i.PriceForElse;

q 是 IEnumerableIQueriable 但不是 int,所以当你调用 ToString() 时它不会返回数字
所以要解决你需要用FirstSingleSum等函数获取单个值
像这样修改代码

var q = from i in dbconnect.tblMaterialTenderAnnouncePrices
        where i.companyId == _comWinnerID && i.MaterialGroupId == materialGroupId
        select i.PriceForElse;

return q.First().ToString();

【讨论】:

    【解决方案2】:

    Input string wasn't in the correct formatInt32.Parse(...) 的一个例外,所以请确保它是一个数字,也许你有一些空格。

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多