Click here to Skip to main content
16,016,168 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Not sure what's wrong with the command lines below.

See the command lines and errors I received.

# Load into datatable
$dt = New-Object System.Data.DataTable
$cmd.CommandText = $sql
$dt.Load($cmd.ExecuteReader("CloseConnection"))


Error
Exception calling "ExecuteReader" with "1" argument(s): "No value given for one or more required parameters."
At C:\test\test_report.ps1:33 char:1
+ $dt.Load($cmd.ExecuteReader("CloseConnection"))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : OleDbException

What I have tried:

The script was provided to me and it was working before and suddenly stopped working.
Posted
Comments
Dave Kreskowiak 10-Oct-23 22:30pm    
The error has nothing to do with the code you posted. It's in the code that sets up the SQL query and any parameters for it. The last step to executing the query is what you posted, but it's not where the error is.

1 solution

Check the content of $sql - if it refers to parameters like this:
SQL
SELECT Description FROM MyTable WHERE ID = @ID
then make sure that you have added the parameterized data to the $cmd object before you try to call ExecuteReader.

And why are you passing ExecuteReader a string? I'm far from a PowerShell expert and I can't find the documentation, but every example I've seen online calls it with no parameters at all ...
 
Share this answer
 
Comments
Richard Deeming 11-Oct-23 4:37am    
"And why are you passing ExecuteReader a string?"

In PowerShell, passing a string is the simplest way to pass an Enum argument to a method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900