【问题标题】:How to check if a Datatable is Null or Nothing如何检查数据表是空还是空
【发布时间】:2012-05-07 01:50:05
【问题描述】:

如何检查DataTable 是否从未设置,这意味着它将是NullNothing?我的意思不是空的DataTable

例如:

Dim dt As DataTable = TryCast(Session("dt"), DataTable)

If dt.Rows.Count <> 0 Then
    'Do something !
End If 

如果 Session("dt") 从未设置过或由于某种原因在内存中丢失,dt.Rows.Count &lt;&gt; 0 将抛出此异常:

对象引用未设置为对象的实例。

【问题讨论】:

标签: vb.net datatable nullreferenceexception


【解决方案1】:

首选:

If dt Is Nothing Then ...

或(类似VB6)

If IsNothing(dt) Then ...

IsNothing Function

【讨论】:

  • 呵呵...当然..为什么我刚才这么傻...谢谢.. :D
  • @LaurenceNyein 你的愚蠢对我很有帮助:)
【解决方案2】:

如果值数据类型是 varbinary(MAX),则使用

if dt.rows(0).item(2) Is DBNull.Value then...

【讨论】:

  • 请重新阅读问题。它明确要求检查何时未设置 DataTable。这意味着您的代码将失败。
【解决方案3】:

dt2 = 数据表 如果 dt2.Rows.Count > 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多