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

I am calling a simple stored procedure written in SQL Server 2008 with the C # parameter, but it displays an error "The procedure or function 'AddYear' expects the parameter '@mYear' which has not been sent.

What is wrong with this code, I tried several things, but did not succeed.

SqlCommand AddEquip = new SqlCommand("AddYear", dbConn); SqlDataReader rdrEquip; SqlParameter mP = new SqlParameter("@mYear",SqlDbType.VarChar ) ; mP.Value = "1990"; AddEquip.Parameters.Add(mP); rdrEquip = AddEquip.ExecuteReader(); 

- The name and type of the parameter are the same as I use in the procedure.

+3
source share
3 answers

It looks like you forgot to set SqlCommand as a stored procedure:

 SqlCommand AddEquip = new SqlCommand("AddYear", dbConn); AddEquip.CommandType = CommandType.StoredProcedure; 
+10
source

You need to add a parameter to your SqlParameter, for example:

 mP = new SqlParameter("@mYear", SqlDbType.VarChar, PARAMETER_HERE); 
0
source

if your Storedprocedure expects a param than you should pass sqlcommand to it

  SqlParameter[] param = new SqlParameter[0]; param[0] = new SqlParameter("@mYear", "1990"); rdrEquip = SqlHelper.ExecuteReader(Con, CommandType.StoredProcedure, "SPC_proc", param); 


I am using the sqlhelper class here.

-3
source

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


All Articles