Reputation: 11851
I have this code here:
public List<CellModel> PostScheduledTasks(List<CellModel> cells)
{
var sortedCells = cells.OrderBy(c => c.sortOrder).ToList();
try
{
using (connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand("PostScheduledTasks", connection))
{
command.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < sortedCells.Count; i++)
{
SqlParameter parameter1 = new SqlParameter("@actualStart", SqlDbType.DateTime);
parameter1.Value = sortedCells[i].actualDate;
parameter1.Direction = ParameterDirection.Input;
command.Parameters.Add(parameter1);
SqlParameter parameter2 = new SqlParameter("@actualFinish", SqlDbType.DateTime);
parameter2.Value = sortedCells[i].finishedDate;
parameter2.Direction = ParameterDirection.Input;
command.Parameters.Add(parameter2);
SqlParameter parameter3 = new SqlParameter("@actualEndDate", SqlDbType.DateTime);
parameter3.Value = sortedCells[i].finishedDate;
parameter3.Direction = ParameterDirection.Input;
command.Parameters.Add(parameter3);
SqlParameter parameter4 = new SqlParameter("@UserDate1", SqlDbType.DateTime);
parameter4.Value = sortedCells[i].scheduledDate;
parameter4.Direction = ParameterDirection.Input;
command.Parameters.Add(parameter4);
SqlParameter parameter5 = new SqlParameter("@IsCompleted", SqlDbType.Bit);
parameter5.Value = (sortedCells[i].selected == true) ? 1 : 0;
parameter5.Direction = ParameterDirection.Input;
command.Parameters.Add(parameter5);
SqlParameter parameter6 = new SqlParameter("@PercentComplete", SqlDbType.Float);
parameter6.Value = (sortedCells[i].selected == true) ? 1 : 0;
parameter6.Direction = ParameterDirection.Input;
command.Parameters.Add(parameter6);
SqlParameter parameter7 = new SqlParameter("@UStmp", SqlDbType.VarChar);
parameter7.Value = sortedCells[i].completedBy;
parameter7.Direction = ParameterDirection.Input;
command.Parameters.Add(parameter7);
SqlParameter parameter8 = new SqlParameter("@ScheduleTaskID", SqlDbType.Int);
parameter8.Value = sortedCells[i].scheduleTaskID;
parameter8.Direction = ParameterDirection.Input;
command.Parameters.Add(parameter8);
SqlParameter parameter9 = new SqlParameter("@SortOrder", SqlDbType.Int);
parameter9.Value = sortedCells[i].sortOrder;
parameter9.Direction = ParameterDirection.Input;
command.Parameters.Add(parameter9);
command.ExecuteNonQuery();
}
UserModel userModel = new UserModel();
userModel.name = "true";
userModel.userName = "true";
return cells;
}
}
}
catch(Exception e)
{
var error = e.Message.ToString();
UserModel nullModel = new UserModel();
nullModel.name = "true";
nullModel.userName = "true";
return cells;
}
finally
{
connection.Close();
}
}
And I have 3 class items. When it does the loop the first time, everything works as expected, but after the second time around the loop, I get this error:
Procedure or function PostScheduledTasks has too many arguments specified.
Do I have to clear something at the end of the loop after each item?
Upvotes: 0
Views: 45
Reputation: 50728
Define the parameters first, then put the for loop after that and set the values directly; currently, if you read through your code, the command has 9 parameters on the first iteration, but 18 on the second, 27 on the third and so on.
Change it to (rough pseudocode):
//Define parameters here
for (..)
{
cmd.Parameters[0].Value = "X";
.
.
}
The benefits to doing it this way is you aren't building up the parameter objects every time, creating and destroying objects frequently when you are going to reuse them...
Upvotes: 1
Reputation: 987
You should clear the parameters held in command.Parameters
property by calling Clear()
method before each iteration.
E.g.
for (int i = 0; i < sortedCells.Count; i++)
{
command.Parameters.Clear();
//your code to add parameters
}
Every time you go through the loop more parameters are added and you are not currently clearing them. So the error is self-explanatory: too many parameters specified.
Upvotes: 3