3
votes

I am calling a simple stored procedure written in SQL Server 2008 with parameter from c# but it display error "Procedure or function 'AddYear' expects parameter '@mYear', which was not supplied."

What's wrong with this code, i tried several things but didn't successed.

    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();  

-- Parameter Name & type are the same i use in the Procedure.

3

3 Answers

12
votes

Seems like you forgot to set the SqlCommand as stored-procedure:

SqlCommand AddEquip = new SqlCommand("AddYear", dbConn);
AddEquip.CommandType = CommandType.StoredProcedure;
0
votes

You need to add the parameter to your SqlParameter e.g:

mP = new SqlParameter("@mYear", SqlDbType.VarChar, PARAMETER_HERE);
-3
votes

if your Storedprocedure is expecting a parament than you have to pass it sqlcommand object

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


i am using sqlhelper class here.