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.  
  3.     param(
  4.  
  5.         [System.Data.SqlClient.SqlConnection]$Conn,
  6.  
  7.         $Sproc,
  8.  
  9.         [hashtable]$Parameters
  10.  
  11.     )
  12.  
  13.     Try{
  14.  
  15.         $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
  16.  
  17.         $SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
  18.  
  19.         $SqlCmd.CommandTimeout = $script:SqlCommandTimeOut;
  20.  
  21.         if ($Conn.State -ne 'Open') { $Conn.open(); }
  22.  
  23.    
  24.  
  25.         $ build the comamnd
  26.  
  27.         $cmd = $Conn.CreateCommand()
  28.  
  29.         $cmd.CommandText = $Sproc
  30.  
  31.         foreach ($p in $Parameters.Keys) {
  32.  
  33.             [Void]$cmd.Parameters.AddWithValue("@$p", $Parameters[$p])
  34.  
  35.         }
  36.  
  37.    
  38.  
  39.         # create the adapter
  40.  
  41.         $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($cmd)
  42.  
  43.    
  44.  
  45.         # fill and return the table.
  46.  
  47.         $dt = New-Object System.Data.DataTable
  48.  
  49.         $x = $SqlAdapter.Fill($dt)
  50.  
  51.                 Write-Host "Rows affected $x" -fore green
  52.  
  53.         $dt  # returns thhe table
  54.  
  55.     }
  56.  
  57.     Catch{
  58.  
  59.         Throw $_
  60.  
  61.     }
  62.  
  63.            
  64.  
  65. } ##Exec-Sproc