Stored procedure or function expects parameter which was not supplied
You need to use SqlCommand.Parameters.AddWithValue
:
cmd.Parameters.AddWithValue("@ParameterName", value);
or SqlCommand.Parameters.Add
for other data types:
cmd.Parameters.Add("@ParameterName", SqlDbType.Int, 5);
cmd.Parameters["@ParameterName"].Value = value;
SqlCommand.Parameters.AddWithValue
replaces the ambiguous overload of Add
that took a string and object parameter. See MSDN for more info.
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);
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.