Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Windows 7, Visual Studio 2017, MVC5 app using .Net Framework 4.8:

I'm trying to call a stored proc that accepts a single nvarchar(450) parameter. When I try to call that stored proc from my code, I get this:

Procedure or function 'StoredProcName' expects parameter '@paramName', which was not supplied.

The SqlParameter array inside the SqlCommand object does indeed contain a single parameter with the correct name, with the expected value, and of the expected SqlDbType at the time I call ExecuteQuery. I examined the above with the debugger, and in an immediate console.

The stored proc does indeed have a single parameter defined like so:

SQL
@paramName NVARCHAR(450)


When I execute the stored proc in SSMS, it works as expected.

What I have tried:

I tried the things described in the question. I present the following observed facts:

In the app:
- The SqlCommand.Parameters object contains a single parameter (as intended)
- The parameter is of the correct type (nvarchar)
- The parameter has the correct name ("@paramName")
- The parameter has the expected string value
- The connection to the database succeeds
- The stored proc was found in the database

In the database:
- The stored proc has a single parameter, of type nvarchar
- The stored proc works when I execute it in SSMS with the same parameter value used in the app
Posted
Updated 1-Jun-22 2:05am
v3
Comments
OriginalGriff 1-Jun-22 0:49am    
Without a relevant code fragment?
We can't do anything ...

It turns out this was a stupid programmer trick (my face is red).

I was calling the wrong DAL method (face is red). I was calling ExecuteQuery with sets the command type to Text. I should have been calling ExecuteStoredProc which sets the command type to StoredProcedure.

When I called the correct method, everything fell into place. :/
 
Share this answer
 
As I said, without seeing your code, we can't really help - but one thing occurs: if your code is passing a parameter value to SQL but you get this error, it is normally because the value passed is null - so use the debugger to see exactly what is begin passed in the relevant SqlCommand.Parameters.Add / SqlCommand.Parameters.AddWithValue call.
If it's null (and that's different from an empty string as you know but some don't) that'll cause your problem.

Of course, if you aren't calling Add / AddWithValue then that'll do it as well, but you're far to experienced to have missed that! :laugh:
 
Share this answer
 
Comments
#realJSOP 1-Jun-22 5:05am    
In my question, I noted that I did in fact examine the cmd.parameters object, that it contained the correctly named parameter and it had the expected value, and was the correct SqlDbType. I even examined all the stuff I could think of in an immediate window. Everything seems fine.
OriginalGriff 1-Jun-22 6:13am    
Connection string? You are connected to the right DB? I'm guessing here ...
#realJSOP 1-Jun-22 6:38am    
Yes. I also tried running the Sql Profiler to see what the database was trying to do, and when I copy the code from the profiler result window into SSMS, it fails with the same error, yet executing the stored proc with "EXEC" still works fine.

BTW, the profiler is trying to do this:

exec sp_executesql N'dbo.UserProfileGet',N'@userID nvarchar(36)',@userID=N'36-character string'
#realJSOP 1-Jun-22 7:00am    
More weirdness - If I make the parameter in the stored proc nullable, it works. I shouldn't have to do that.
OriginalGriff 1-Jun-22 7:13am    
Why are you using sp_executesql? It expects a SQL statement, not an SP name which may be confusing it.
Try:
exec dbo.UserProfileGet @userID=N'36-character string'

[edit] Forgot to remove enough from your SQL [/edit]

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