Le SqlParameter est déjà contenue par un autre SqlParameterCollection - commande.Les paramètres.Clear() ne fonctionne pas

Je suis désespérée ici. Je ne peux pas comprendre pourquoi cette exception se produire. J'ai cherché et lu à propos de ce problème et il semble que tout le monde a réussi à le résoudre avec la commande.Les paramètres.Clear(), mais cela ne fonctionne pas pour moi. Voici mon code:

    public class ProductsDataAccess : Connection 
{
public static SqlParameter[] createParams(Products prod)
{
SqlParameter idParam = new SqlParameter("@id", prod.prodID);
idParam.SqlDbType = System.Data.SqlDbType.VarChar;
idParam.Size = 45;
idParam.IsNullable = false;
idParam.Direction = ParameterDirection.Input;
SqlParameter nameParam = new SqlParameter("@name", prod.prodName);
nameParam.SqlDbType = System.Data.SqlDbType.VarChar;
nameParam.Size = 45;
nameParam.IsNullable = false;
nameParam.Direction = ParameterDirection.Input;
SqlParameter demandParam = new SqlParameter("@demand", prod.demand);
demandParam.SqlDbType = System.Data.SqlDbType.Int;
demandParam.IsNullable = false;
demandParam.Direction = ParameterDirection.Input;
SqlParameter demandLTParam = new SqlParameter("@demandLT", prod.demandLT);
demandLTParam.SqlDbType = System.Data.SqlDbType.Int;
demandLTParam.IsNullable = false;
demandLTParam.Direction = ParameterDirection.Input;
SqlParameter leadTimeParam = new SqlParameter("@leadTime", prod.leadTime);
leadTimeParam.SqlDbType = System.Data.SqlDbType.Int;
leadTimeParam.IsNullable = false;
leadTimeParam.Direction = ParameterDirection.Input;
SqlParameter fixedCoastParam = new SqlParameter("@k", prod.k);
fixedCoastParam.SqlDbType = System.Data.SqlDbType.Float;
fixedCoastParam.IsNullable = false;
fixedCoastParam.Direction = ParameterDirection.Input;
SqlParameter cParam = new SqlParameter("@c", prod.c);
cParam.SqlDbType = System.Data.SqlDbType.Float;
cParam.IsNullable = false;
cParam.Direction = ParameterDirection.Input;
SqlParameter chPrecentageParam = new SqlParameter("@chPrecent", prod.precentOfC);
chPrecentageParam.SqlDbType = System.Data.SqlDbType.Int;
chPrecentageParam.IsNullable = false;
chPrecentageParam.Direction = ParameterDirection.Input;
SqlParameter chParam = new SqlParameter("@ch", prod.ch);
chParam.SqlDbType = System.Data.SqlDbType.Float;
chParam.IsNullable = false;
chParam.Direction = ParameterDirection.Input;
SqlParameter optimalQParam = new SqlParameter("@q", prod.q);
optimalQParam.SqlDbType = System.Data.SqlDbType.Int;
optimalQParam.IsNullable = false;
optimalQParam.Direction = ParameterDirection.Input;
SqlParameter annualChParam = new SqlParameter("@annualCh", prod.annualCh);
annualChParam.SqlDbType = System.Data.SqlDbType.Float;
annualChParam.IsNullable = false;
annualChParam.Direction = ParameterDirection.Input;
SqlParameter annualKParam = new SqlParameter("@annualK", prod.annualK);
annualKParam.SqlDbType = System.Data.SqlDbType.Float;
annualKParam.IsNullable = false;
annualKParam.Direction = ParameterDirection.Input;
SqlParameter optimalTACParam = new SqlParameter("@tac", prod.tac);
optimalTACParam.SqlDbType = System.Data.SqlDbType.Float;
optimalTACParam.IsNullable = false;
optimalTACParam.Direction = ParameterDirection.Input;
SqlParameter rParam = new SqlParameter("@r", prod.r);
rParam.SqlDbType = System.Data.SqlDbType.Int;
rParam.IsNullable = false;
rParam.Direction = ParameterDirection.Input;
SqlParameter inventoryLvlParam = new SqlParameter("@invLvl", prod.inventoryLvl);
inventoryLvlParam.SqlDbType = System.Data.SqlDbType.Int;
inventoryLvlParam.IsNullable = false;
inventoryLvlParam.Direction = ParameterDirection.Input;
SqlParameter statusParam = new SqlParameter("@status", prod.status);
statusParam.SqlDbType = System.Data.SqlDbType.VarChar;
statusParam.Size = 10;
statusParam.IsNullable = false;
statusParam.Direction = ParameterDirection.Input;
SqlParameter[] paramList = {idParam, nameParam, demandParam, demandLTParam,
leadTimeParam, fixedCoastParam, cParam,
chPrecentageParam, chParam, optimalQParam,
annualChParam, annualKParam, optimalTACParam, 
rParam, inventoryLvlParam, statusParam };
return paramList;
}
public static void insertProduct(Products product)
{
String comStr = "insert into Products(prodID, name, demand, demandLT, leadTime, fixedCoast, " + 
"c, chAsPrecentage, ch, optimalQ, annualCh, annualK, optimalTAC, r, inventoryLvl, prodStatus) " + 
"values(@id, @name, @demand, @demandLT, @leadTime, @k, @c, @chPrecent, " + 
"@ch, @q, @annualCh, @annualK, @tac, @r, @invLvl, @status)";
createConncetion();
SqlCommand command = new SqlCommand(comStr, con);
command.Parameters.AddRange(createParams(product));
openConnection();
command.ExecuteNonQuery();
closeConnection();
command.Parameters.Clear()
command.Dispose();
}
public static DataTable getAllProducts()
{
String comStr = "select prodID, name, demand, demandLT, leadTime, fixedCoast, c, chAsPrecentage, " + 
"ch, optimalQ, annualCh, annualK, optimalTAC, r, inventoryLvl, prodStatus from Products";
createConncetion();
SqlCommand command = new SqlCommand(comStr, con);
DataTable table = new DataTable("allProduct");
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand=command;
openConnection();
adapter.Fill(table);
command.Dispose();
closeConnection();
return table;
}
}

Voici l'autre classe dans laquelle je suis à l'aide de sqlParameters:

    public class StatisticsDataAccess : Connection
{
public static SqlParameter[] createParams(Statistics stat, string prodID)
{
SqlParameter idParam = new SqlParameter("@id", prodID);
idParam.SqlDbType = System.Data.SqlDbType.VarChar;
idParam.Size = 45;
idParam.IsNullable = false;
idParam.Direction = ParameterDirection.Input;
SqlParameter m1Param = new SqlParameter("@m1", stat.month1);
m1Param.SqlDbType = System.Data.SqlDbType.Int;
m1Param.IsNullable = false;
m1Param.Direction = ParameterDirection.Input;
SqlParameter m2Param = new SqlParameter("@m2", stat.month2);
m2Param.SqlDbType = System.Data.SqlDbType.Int;
m2Param.IsNullable = false;
m2Param.Direction = ParameterDirection.Input;
SqlParameter m3Param = new SqlParameter("@m3", stat.month3);
m3Param.SqlDbType = System.Data.SqlDbType.Int;
m3Param.IsNullable = false;
m3Param.Direction = ParameterDirection.Input;
SqlParameter m4Param = new SqlParameter("@m4", stat.month4);
m4Param.SqlDbType = System.Data.SqlDbType.Int;
m4Param.IsNullable = false;
m4Param.Direction = ParameterDirection.Input;
SqlParameter m5Param = new SqlParameter("@m5", stat.month5);
m5Param.SqlDbType = System.Data.SqlDbType.Int;
m5Param.IsNullable = false;
m5Param.Direction = ParameterDirection.Input;
SqlParameter m6Param = new SqlParameter("@m6", stat.month6);
m6Param.SqlDbType = System.Data.SqlDbType.Int;
m6Param.IsNullable = false;
m6Param.Direction = ParameterDirection.Input;
SqlParameter m7Param = new SqlParameter("@m7", stat.month7);
m7Param.SqlDbType = System.Data.SqlDbType.Int;
m7Param.IsNullable = false;
m7Param.Direction = ParameterDirection.Input;
SqlParameter m8Param = new SqlParameter("@m8", stat.month8);
m8Param.SqlDbType = System.Data.SqlDbType.Int;
m8Param.IsNullable = false;
m8Param.Direction = ParameterDirection.Input;
SqlParameter m9Param = new SqlParameter("@m9", stat.month9);
m9Param.SqlDbType = System.Data.SqlDbType.Int;
m9Param.IsNullable = false;
m9Param.Direction = ParameterDirection.Input;
SqlParameter m10Param = new SqlParameter("@m10", stat.month10);
m10Param.SqlDbType = System.Data.SqlDbType.Int;
m10Param.IsNullable = false;
m10Param.Direction = ParameterDirection.Input;
SqlParameter m11Param = new SqlParameter("@m11", stat.month11);
m11Param.SqlDbType = System.Data.SqlDbType.Int;
m11Param.IsNullable = false;
m11Param.Direction = ParameterDirection.Input;
SqlParameter m12Param = new SqlParameter("@m12", stat.month12);
m12Param.SqlDbType = System.Data.SqlDbType.Int;
m12Param.IsNullable = false;
m12Param.Direction = ParameterDirection.Input;
SqlParameter[] paramList = {idParam, m1Param, m2Param, m3Param, m4Param,
m5Param, m6Param, m7Param, m8Param, m8Param,
m10Param, m11Param, m12Param };
return paramList;
}
public static void insertStatistic(Statistics stat, string prodID)
{
String comStr = "insert into LTDStatistic(prodID, month1, month2, month3, month4, month5, " +
"month6, month7, month8, month9, month10, month11, month12)" +
"values(@id, @m1, @m2, @m3, @m4, @m5, @m6, @m7, @m8, @m9, @m10, @m11, @m12)";
createConncetion();
SqlCommand command = new SqlCommand(comStr, con);
command.Parameters.AddRange(createParams(stat, prodID));
openConnection();
command.ExecuteNonQuery();
closeConnection();
command.Parameters.Clear();
command.Dispose();
}
}

Service WCF utilise des méthodes de ces classes dans le but de modifier une base de données

        public void create(ProductsData product)
{
Statistics statDLT = new Statistics(product.demandLTStat.month1, product.demandLTStat.month2,
product.demandLTStat.month3, product.demandLTStat.month4, product.demandLTStat.month5,
product.demandLTStat.month6, product.demandLTStat.month7, product.demandLTStat.month8,
product.demandLTStat.month9, product.demandLTStat.month10, product.demandLTStat.month11,
product.demandLTStat.month12);
Products newProd = new Products(product.prodID, product.prodName,
product.leadTime, product.k, product.c, product.precentOfC, 
product.demand, statDLT);
newProd.callculate();
newProd.determineR();
ProductsDataAccess.insertProduct(newProd);
//String plsWork = newProd.prodID;
StatisticsDataAccess.insertStatistic(newProd.demandLTStat, newProd.prodID);
}

Peut-être Im manque un aspect important de la façon dont les choses fonctionnent ... je ne sais pas. J'espère que quelqu'un pourra m'aider, et désolé pour le long de segments de code.

  • Avez-vous réglé le code pour afficher le paramètre qui est à l'origine du problème?
  • Oui je l'ai fait, et j'ai vu que la collection de paramètres de la commande est vide à la fin de chaque méthode. Je ne peux pas voir l'exacte paramètre qui provoque l'exception qui se produit à la ligne suivante: commande.Les paramètres.AddRange(createParams(stat, prodID)); je vais essayer d'ajouter les paramètres un par un et voir si quelque chose de nouveau apparaît.
  • Vous avez besoin de placer à l'aide de blocs de votre connexion et vos commandes. Ce sera toujours Disposer de méthodes sont appelées correctement. La classe est l'erreur ne se produise?
  • StatisticsDataAccess