C # Stored procedure or function expects a parameter that is not provided

I am new to C # and I am trying to set up a stored procedure call in my database that takes one parameter.

I get the error message "Procedure or function" SP_getName "expects parameter" @username ", which was not specified."

My stored procedure works fine when I supply it with a parameter, and I run it through SQL Management Studio.

GO DECLARE @return_value int EXEC @return_value = [dbo].[SP_getName] @username = 'bob101' SELECT 'Return Value' = @return_value GO 

However, when I try to call it, the error is related to the way I pass this parameter, but I can not determine what the problem is.

  //create a sql command object to hold the results of the query SqlCommand cmd = new SqlCommand(); //and a reader to process the results SqlDataReader reader; //Instantiate return string string returnValue = null; //execute the stored procedure to return the results cmd.CommandText = "SP_getName"; //set up the parameters for the stored procedure cmd.Parameters.Add("@username", SqlDbType.NVarChar).Value = "bob101"; cmd.CommandType = CommandType.Text; cmd.Connection = this.Connection; // then call the reader to process the results reader = cmd.ExecuteReader(); 

Any help in determining my mistake would be greatly appreciated!

I also tried looking at these two posts, but I had no luck:

A stored procedure or function expects a parameter that is not specified

A procedure or function expects a parameter that has not been provided

Thanks!

+5
source share
3 answers

You stated:

 cmd.CommandType = CommandType.Text; 

So you just do:

 SP_getName 

Which works because it is the first statement in the package, so you can call the procedure without EXECUTE , but in fact you do not enable this parameter. Change it to

 cmd.CommandType = CommandType.StoredProcedure; 

Or you can change your CommandText to:

 EXECUTE SP_getName @username; 

As an additional note, you should avoid using the sp_ prefix for stored procedures.

And one more note - use using with IDisposable objects to make sure they are properly disposed of:

 using (var connection = new SqlConnection("ConnectionString")) using (var cmd = new new SqlCommand("SP_getName", connection)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@username", SqlDbType.NVarChar).Value = "bob101"; connection.Open(); using (var reader = command.ExecuteReader()) { while (reader.Read()) { // Do something } } } 
+13
source

Try removing @ :

 cmd.Parameters.Add("username", SqlDbType.NVarChar).Value = "bob101"; 
0
source

I had this problem, but it was not about the Command Type parameter name. My problem was that when C # calls SP, for every parameter that doesn't matter, the default keyword passes (I found it in SQL Profiler):

 ... @IsStop=0,@StopEndDate=default,@Satellite=0, ... 

in my case, my Type parameter was a DateTime:

 @StopEndDate datetime 

. I solved my problem by setting the default value for this parameter in the Stored Procedure:

 @StopEndDate datetime=null 
0
source

Source: https://habr.com/ru/post/1205949/


All Articles