Krishna Kumar

Krishna Kumar

  • NA
  • 135
  • 38.9k

The SqlParameter is already contained by another SqlParamet

Nov 12 2016 1:25 AM
Hello...
 
i am passing the stored procedure name and parameter collection to this method. but while executing it is giving the error, The SqlParameter is already contained by another SqlParameter. eventhough i have added the parameters.clear() function to clear the parameters. but it is giving the same error
 
public void executeSP(string spname, SqlParameterCollection sp)
{
int retobj = 0;
//open connection
con.Open();
//execute SP
cmd = new SqlCommand(spname, con);
cmd.CommandType = CommandType.StoredProcedure;
if (sp.Count > 0)
{
foreach (SqlParameter p in sp)
{
cmd.Parameters.Add(p);
cmd.Parameters.Clear();
}
}
retobj=cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
//Close connection
con.Close();

Answers (8)