Home > Net >  Already included in another SqlParameterCollection SqlParameter.
Already included in another SqlParameterCollection SqlParameter.

Time:11-12

The following code:
Public int DianCai (int orderId, int dishId)
{
//the time of you need to determine the order, this order ever point this dish, if points, only need to number of + 1, if there is no point, you need to add a line of
String SQL="select count (*) from orderdetailinfo where orderid=@ orderid and dishid=@ dishid";
SqlParameter [] ps=
{
New SqlParameter (" @ orderid ", orderid),
New SqlParameter (" @ dishid "dishid)
};
Int count=the Convert. ToInt32 (SqlServerHelper ExecuteScalar (SQL, ps));

If (count> 0)
{
//if this order the food, the number of + 1
SQL="update orderdetailinfo set count=count + 1 where orderid=@ orderid and dishid=@ dishid";
}
The else
{
//if there is no point to this dish, add
SQL="insert into orderdetailinfo (orderid, dishid, count) values (@ orderid, @ dishid, 1)";
}

Return SqlServerHelper. ExecuteNonQuery (SQL, ps);
}

Public static int ExecuteNonQuery (string SQL, params SqlParameter [] ps)
{
Using (SqlConnection SQLCN=new SqlConnection (connString))
{
SqlCommand SQLCMD=new SqlCommand (SQL, SQLCN);
SQLCN. The Open ();

SQLCMD. The Parameters. AddRange (ps);
Int number=SQLCMD. ExecuteNonQuery ();
SQLCMD. The Parameters. The Clear ();
Return the number;
}
}

Running like this, every time an error:

"System. ArgumentException" type of untreated anomalies in the System. Data.
in the DLL
Other information: already included in another SqlParameterCollection SqlParameter,

A great god, please give directions,

CodePudding user response:

Your variable ps is used twice, try to regenerate a, call again,

CodePudding user response:

# 1 is correct,
Once upon a time, I also met this problem,
Every time need to instantiate a new sqlparameter,

CodePudding user response:

Want to see your ExecuteScalar code, it is estimated that no SQLCMD. The Parameters, the Clear (); This sentence

CodePudding user response:

SqlParameter [] ps=
{
New SqlParameter (" @ orderid ", orderid),
New SqlParameter (" @ dishid "dishid)
};
Int count=the Convert. ToInt32 (SqlServerHelper ExecuteScalar (SQL, ps));
Ps=
{
New SqlParameter (" @ orderid ", orderid),
New SqlParameter (" @ dishid "dishid)
};
  •  Tags:  
  • C#
  • Related