Reputation: 53
I have a powershell script that accepts user input that I plan to use to execute a stored procedure. The sproc accepts multiple parameters to customize the data that is returned. However, when I run this powershell code, I get the following error:
Cannot index into a null array.
At C:\Report.ps1:119 char:42
+ foreach ($Row in $global:DataSet.Tables[ <<<< 0].Rows)
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
This is the powershell code I'm using to execute the call to the stored procedure.
# Create Shared Helper Objects
$nl = [Environment]::NewLine
function msg
{
param($message,$fgcolor="white",$bgcolor="black")
$output = Write-Host $message -ForegroundColor $fgcolor -BackgroundColor $bgcolor
write-output $output
}
Set-Location "C:\"
$month = Read-Host "Enter Report Month"
$year = Read-Host "Enter Report Year"
msg "$nl Choose a product" "White"
$prompt = ' [D] Desktop [M] Mobile'
switch (Read-Host $prompt) {
"D" {$product = "Desktop"; break}
"M" {$product = "Mobile"; break}
}
msg "$nl Select a Make" "White"
$prompt = ' [F] First [L] Last '
switch (Read-Host $prompt) {
"F" {$SiteMake = "First"; break}
"L" {$SiteMake = "Last"; break}
}
msg "$nl Select a Product" "White"
$prompt = ' [E] Extra [B] Basic'
switch (Read-Host $prompt) {
"E" {$SitePackage = "Extra"; break}
"B" {$SitePackage = "Basic"; break}
}
# Create Database Connection Function
function ConnectSproc
{
param ($SQLServer, $SQLDBName, $SqlQuery)
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
# Report Month
$SqlCmd.Parameters.Add("@ReportMonth",[system.data.SqlDbType]::VarChar) | out-Null
$SqlCmd.Parameters["@ReportMonth"].Direction = [system.data.ParameterDirection]::Input
$SqlCmd.Parameters["@ReportMonth"].value = $month
# Report Year
$SqlCmd.Parameters.Add("@ReportYear",[system.data.SqlDbType]::VarChar) | out-Null
$SqlCmd.Parameters["@ReportYear"].Direction = [system.data.ParameterDirection]::Input
$SqlCmd.Parameters["@ReportYear"].value = $year
# Product
$SqlCmd.Parameters.Add("@Product",[system.data.SqlDbType]::VarChar) | out-Null
$SqlCmd.Parameters["@Product"].Direction = [system.data.ParameterDirection]::Input
$SqlCmd.Parameters["@Product"].value = $product
# Site Make
$SqlCmd.Parameters.Add("@SiteMake",[system.data.SqlDbType]::VarChar) | out-Null
$SqlCmd.Parameters["@SiteMake"].Direction = [system.data.ParameterDirection]::Input
$SqlCmd.Parameters["@SiteMake"].value = $SiteMake
# Site Package
$SqlCmd.Parameters.Add("@SitePackage",[system.data.SqlDbType]::VarChar) | out-Null
$SqlCmd.Parameters["@SitePackage"].Direction = [system.data.ParameterDirection]::Input
$SqlCmd.Parameters["@SitePackage"].value = $SitePackage
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet) | Out-Null
$SqlConnection.Close()
$DataSet.Tables[0]
}
ConnectSproc "servername" "databasename" "mysproc"
I'm at a loss. Any help would be appreciated!
Update: it actually looks like the procedure isn't even executing correctly. That would probably explain the error that I'm getting. Now I'm not sure why the sproc isn't executing.
Upvotes: 1
Views: 1052
Reputation: 53
I figured it out. It ended up being a scoping issue with the $DataSet variable being created inside the function and not prefixed with "$global:"
To fix it, I changed
$DataSet = New-Object System.Data.DataSet
to $global:DataSet = New-Object System.Data.DataSet
and referenced it as $global:DataSet.Tables[0]
outside of the function.
Upvotes: 1
Reputation: 9500
Maybe the call to Fill()
failed. Temporarily comment out the | Out_Null
part of $SqlAdapter.Fill($DataSet) | Out-Null
, and see if that sheds any light.
Upvotes: 0