Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The SqlParameter is already contained by another SqlParameterCollectio

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
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:

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top