【问题标题】:Why throwing an error : specified cast is not valid?为什么抛出错误:指定的演员表无效?
【发布时间】:2015-07-08 17:56:56
【问题描述】:

我在 sql 中使用的 'userid' 是 int 数据类型,而在后端(.cs)中也使用了相同的 int 数据类型,那么为什么会抛出这个错误:'specified cast is not valid'

var userRowsCustId = dsCustReg.Tables[0]
                              .AsEnumerable()
                              .Select(r => r.Field<int>("AccountNo"));

var allRowsCustId = dsAllTables.Tables[0]
                               .AsEnumerable()
                               .Select(r => r.Field<int>("UserId"));

IEnumerable<int> allIntersectingCustIds = userRowsCustId.Except(allRowsCustId);

if (allIntersectingCustIds.Any())
{
}

这个错误的解决方法是什么?

r =&gt; r.Field&lt;int&gt;("AccountNo") 抛出错误

【问题讨论】:

  • 您是否使用SqlDataAdapter 填写表格或如何填写DataTableAccountNo 也是 int 列吗?查看调试器中的dsCustReg.Columns
  • 数据库中AccountNoUserId是什么数据类型?
  • 另外,您的代码似乎存在命名或逻辑问题。相交通常意味着它们共享相同的值,但Except 返回所有不存在于客户行中的用户行。也许你想使用Intersect
  • 也许你的字段是int? (nullable int)
  • userid 和 account no 在前端我显示 accno 和在后端使用用户 id 相同

标签: c# sql


【解决方案1】:

我明白了..当我用 double 替换成时它接受了

【讨论】:

    猜你喜欢
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    相关资源
    最近更新 更多