Reputation: 1093
I'm having a strange issue with entering data into a SQL Server 2008 database, the error tells me that
System.Data.SqlClient.SqlException was unhandled HResult=-2146232060 Message=The variable name '@comments' has already been declared. Variable names must be unique within a query batch or stored procedure. Must declare the scalar variable "@employeeId".
Problem is... I did declare both of them, am I missing something?
public bool Addfix(DateTime receiveDate, int clientId, int modelID, string problem, string comment, int employeeId, float priceOffer, float price)
{
_cmd.CommandText ="INSERT INTO fixes(receiveDate, clientId, modelID, problem, comments, employeeId, priceOffer, price, returned) VALUES (@receiveDate, @clientId, @modelID, @problem, @comments, @employeeId, @priceOffer,@price,@returned);";
_cmd.Parameters.Add("@receiveDate", SqlDbType.Date).Value = receiveDate;
_cmd.Parameters.AddWithValue("@clientId", clientId);
_cmd.Parameters.AddWithValue("@modelID", modelID);
_cmd.Parameters.AddWithValue("@problem", problem);
_cmd.Parameters.AddWithValue("@comments", comment);
_cmd.Parameters.AddWithValue("@employeeId", employeeId);
_cmd.Parameters.AddWithValue("@priceOffer", priceOffer);
_cmd.Parameters.AddWithValue("@price", price);
_cmd.Parameters.AddWithValue("@returned ", 0);
_con.Open();
_cmd.ExecuteNonQuery();
_con.Close();
return true;
}
Upvotes: 1
Views: 488
Reputation: 11
Looking at your function definition, it seems your sqlcommand/sqlconnectin objects are either class level or global. In your case it seems you used the _cmd object which had @comments parameter and did not get cleared before execution of this method. Always discourage to use global sqlcommand and sqlconnection objects, but for some unavoidable reason you need to use global sqlcommand, its always good practice to clear the parameters of command objects, e.g. sqlcommandObject.Parameters.Clear().
Upvotes: 1