【问题标题】:Empty SQL query/Value in Gridview ErrorGridview 错误中的空 SQL 查询/值
【发布时间】:2018-02-17 11:24:42
【问题描述】:

我正在构建一个预订应用程序并且我正在尝试修复一个错误 - 我将每个工作站预订了 x 天,因此 0 个工作站应该显示在我的 GridView 中 - 但是当我在日历中单击那一天时,程序崩溃并给出我这个错误

  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

    Source Error: 


  Line 246:            gvAvailable.DataSource = ds.Tables[0];
  Line 247:            gvAvailable.DataBind();
  Line 248:            gvAvailable.HeaderRow.Cells[0].Text = "Workstation";
  Line 249:            gvAvailable.HeaderRow.Cells[1].Text = "Location";
  Line 250:        }

我测试了选择该日期时它正在执行的查询,这是一个正确的空查询,因为所有车站都已预订,但程序崩溃了。有帮助吗?

【问题讨论】:

  • 在设置数据源之前先检查表是否为NOTHING/NULL
  • 你是如何填充你的数据集的?在我看来,ds.Tables 可能是空的,所以ds.Tables[0] 导致一个空引用。
  • if (ds.Tables != null) { try { gvAvailable.DataSource = ds.Tables[0]; gvAvailable.DataBind(); gvAvailable.HeaderRow.Cells[0].Text = "工作站"; gvAvailable.HeaderRow.Cells[1].Text = "位置"; } 捕捉 (ArgumentNullException) { } }

标签: c# asp.net tsql gridview postback


【解决方案1】:

实现了检查数据集是否为空的正确语法

 ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 2011-05-01
    • 2018-04-22
    • 2011-02-14
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多