Testing DataSet and/or DataTable is null

Dave Gray 471 Reputation points


Can anyone please tell me how I can test a DataSet contains at least a .Tables[0] or a DataTable is not null?

For in the mocked up example, where 1=2 is always going to return null and therefore both the commands fail.

In production I would be returning null 99% of the time so I need to test for it and move on when null.


$DS = (Invoke-Sqlcmd -ServerInstance "server" -Username "user" -Password "password" -Database "database" -Query "select 1,2,3 where 1=2" -As DataSet)

$DT = (Invoke-Sqlcmd -ServerInstance "server" -Username "user" -Password "password" -Database "database" -Query "select 1,2,3 where 1=2" -As DataSet).Tables[0]

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,630 questions
No comments
{count} votes

Accepted answer
  1. Dave Gray 471 Reputation points

    The obvious answer is below :-)

    if ($DS -ne $null)

0 additional answers

Sort by: Most helpful