Stored procedure or function expects parameter which was not supplied

后端 未结 9 1151
梦谈多话
梦谈多话 2021-01-17 08:47

I am trying to insert data into a SQL Server database by calling a stored procedure, but I am getting the error

*Procedure or function \'Insertion\'

相关标签:
9条回答
  • 2021-01-17 09:24

    For others : I just faced the same error because one of my parameters was null. We need to check for it such as :

    command.Parameters.AddWithValue("@phone", (object)phone?? DBNull.Value);
    
    0 讨论(0)
  • 2021-01-17 09:27

    Just a headsup, it might save someone a lot of time soul searching. If you have followed the recommendation here, like using AddWithValue in order to pass a paramter on, and you have everything verified and yet you are still getting the error message "Not supplied", check whether you have set the CommandType property of the command object to CommandType.StoredProcedure.

    Not setting this property incurs the same message, believe me! Hope it helps someone.

    0 讨论(0)
  • 2021-01-17 09:33

    Avoid parameters that have no value with the parameter set being DBNull.value

    0 讨论(0)
  • 2021-01-17 09:36

    Your Insertion stored procedure is expecting @Emp_no (along with about 15 other parameters). You cannot call the stored procedure without passing the parameters.

    Take a look at this site for reference:

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

    Everywhere you're defining variables, use Parameters.AddWithValue instead:

    cmd.Parameters.AddWithValue("@Emp_no ", Convert.ToInt32(txtbx_Empno.Text));
    
    0 讨论(0)
  • 2021-01-17 09:39

    This is how it can be done

    using (var cmd = new SqlCommand("STORED_PROCEDURE_NAME", SqlConnection))
    {
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.Parameters.AddWithValue("@PARAM_NAME", PARAM_VALUE);
    }
    

    Notice that AddWithValue, and CommandType.StoredProcedure both are essential.

    0 讨论(0)
  • 2021-01-17 09:43

    Same error message still these days scoping multiple problems - my issue was passing a null value to the parameter. The answer is to null check like so:

    parameter.Value = (object)yourParamVal ?? DBNULL.Value;
    
    0 讨论(0)
提交回复
热议问题