Re: pipeline stopped error when closing out-gridview
Posted: Tue Oct 04, 2016 1:04 pm
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.
- function Exec-Sproc{
- param(
- [System.Data.SqlClient.SqlConnection]$Conn,
- $Sproc,
- [hashtable]$Parameters
- )
- Try{
- $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
- $SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
- $SqlCmd.CommandTimeout = $script:SqlCommandTimeOut;
- if ($Conn.State -ne 'Open') { $Conn.open(); }
- $ build the comamnd
- $cmd = $Conn.CreateCommand()
- $cmd.CommandText = $Sproc
- foreach ($p in $Parameters.Keys) {
- [Void]$cmd.Parameters.AddWithValue("@$p", $Parameters[$p])
- }
- # create the adapter
- $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($cmd)
- # fill and return the table.
- $dt = New-Object System.Data.DataTable
- $x = $SqlAdapter.Fill($dt)
- Write-Host "Rows affected $x" -fore green
- $dt # returns thhe table
- }
- Catch{
- Throw $_
- }
- } ##Exec-Sproc