I have been trying to find a simple way to create parameters to pass from method to method but can't seem to make it work.
Keep getting different problems. Not sure why I get this problem.
Here I have created a method to copy parameters to an SqlParameter array and passing that but I still get the error:
Here I take the parameter out of an SqlCommand.Parameters object and pass it:
I would assume that the parameter array is now not part of the parameter array.
But when I do:
The command.Parameters.Add(parameter) gives me the error.
How is that?
First I moved it from the collection to an array and then moved the parameter to an individual SqlParameter variable.
How can I move the parameters into an array so that this doesn't happen?
Thanks,
Tom
Keep getting different problems. Not sure why I get this problem.
Here I have created a method to copy parameters to an SqlParameter array and passing that but I still get the error:
Code:
public static SqlParameter[] GetSqlParameterArray(SqlParameterCollection spc)
{
ArrayList spArray = new ArrayList();
foreach (SqlParameter sp in spc)
{
spArray.Add(sp);
}
return (SqlParameter[])spArray.ToArray(typeof(SqlParameter));
}
Here I take the parameter out of an SqlCommand.Parameters object and pass it:
Code:
SqlParameter[] sp = database.GetSqlParameterArray(sc.Parameters);
I would assume that the parameter array is now not part of the parameter array.
But when I do:
Code:
private SqlCommand BuildQueryCommand(string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = new SqlCommand(storedProcName, myConnection);
command.CommandType = CommandType.StoredProcedure;
SqlParameter parameter = default(SqlParameter);
if ((parameters != null)) {
foreach (SqlParameter parameter_loopVariable in parameters)
{
parameter = parameter_loopVariable;
command.Parameters.Add(parameter);
}
}
return command;
}
The command.Parameters.Add(parameter) gives me the error.
How is that?
First I moved it from the collection to an array and then moved the parameter to an individual SqlParameter variable.
How can I move the parameters into an array so that this doesn't happen?
Thanks,
Tom