Stored Procedure or Function Expects Parameter That's Purportedly Supplied
When calling a stored procedure to insert data into a SQL Server database, you may encounter the error "Procedure or function ' stored procedure name ' expects parameter '@parameter name', which was not supplied." This error message can be perplexing, especially if you have verified that the stored procedure has the correct parameters and that they are being supplied in your code.
Troubleshooting the Error
To resolve this issue, thoroughly review the following:
Sample Code Update
Here's an updated version of your code that explicitly specifies the command type and parameter values:
public void SHOWuser(string userName, string password, string emailAddress, List<int> preferences) { using var dbcon = new SqlConnection(conn); using var cmd = new SqlCommand(); cmd.Connection = dbcon; cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.CommandText = "SHOWuser"; cmd.Parameters.AddWithValue("@userName", userName); cmd.Parameters.AddWithValue("@password", password); cmd.Parameters.AddWithValue("@emailAddress", emailAddress); dbcon.Open(); int i = Convert.ToInt32(cmd.ExecuteScalar()); cmd.Parameters.Clear(); cmd.CommandText = "tbl_pref"; foreach (int preference in preferences) { cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@userID", Convert.ToInt32(i)); cmd.Parameters.AddWithValue("@preferenceID", Convert.ToInt32(preference)); cmd.ExecuteNonQuery(); } }
By carefully inspecting the parameter names, data types, command type, and parameter ordering, you can alleviate this error and successfully execute your stored procedure.
The above is the detailed content of Why Does My SQL Stored Procedure Throw a 'Procedure or function '...' expects parameter '@parameter name', which was not supplied' Error?. For more information, please follow other related articles on the PHP Chinese website!