Page 2 of 2

Re: pipeline stopped error when closing out-gridview

Posted: Tue Oct 04, 2016 1:04 pm
by jvierra
Your function is not well formed and would work better this way as it will return a useful object that is complete, empty or null.
  1. function Exec-Sproc{
  2.     param(
  3.         [System.Data.SqlClient.SqlConnection]$Conn,
  4.         $Sproc,
  5.         [hashtable]$Parameters
  6.     )
  7.     Try{
  8.         $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
  9.         $SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
  10.         $SqlCmd.CommandTimeout = $script:SqlCommandTimeOut;
  11.         if ($Conn.State -ne 'Open') { $Conn.open(); }
  12.    
  13.         $ build the comamnd
  14.         $cmd = $Conn.CreateCommand()
  15.         $cmd.CommandText = $Sproc
  16.         foreach ($p in $Parameters.Keys) {
  17.             [Void]$cmd.Parameters.AddWithValue("@$p", $Parameters[$p])
  18.         }
  19.    
  20.         # create the adapter
  21.         $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($cmd)
  22.    
  23.         # fill and return the table.
  24.         $dt = New-Object System.Data.DataTable
  25.         $x = $SqlAdapter.Fill($dt)
  26.                 Write-Host "Rows affected $x" -fore green
  27.         $dt  # returns thhe table
  28.     }
  29.     Catch{
  30.         Throw $_
  31.     }
  32.            
  33. } ##Exec-Sproc