I get an error
Procedure or function \"myStoreProcNameHere\" has too many arguments specified.
Below is my stored procedure and
I've ran into this error message before when I was calling a stored procedure from within a loop (foreach).
The cause of the exception that I received was due to the fact that I wasn't clearing the Parameter's after each iteration of the loop.
For example: This code throws:
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "MySproc";
foreach (MyClass c in myClasses)
{
cmd.Parameters.AddWithValue("@val1", c.val1);
cmd.Parameters.AddWithValue("@val2", c.val2);
var result = MyDbManager.instance.ExecuteScalarQuery(cmd);
}
}
The fix was to clear the parameters:
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "MySproc";
foreach (MyClass c in myClasses)
{
cmd.Parameters.AddWithValue("@val1", c.val1);
cmd.Parameters.AddWithValue("@val2", c.val2);
var result = MyDbManager.instance.ExecuteScalarQuery(cmd);
cmd.Parameters.Clear();
}
}