I want to update the price of items in the database. I am extracting a list of items from another list( of class type having other information also). After that I am sending it(item number) one by one as a stored procedure parameter to get the price updated.
But I am getting a Procedure or function sp_updateItems has too many arguments specified exception even though i am sending only one item as a parameter at a time and my stored procedure also accepts only one parameter.
My stored procedure is
But I am getting a Procedure or function sp_updateItems has too many arguments specified exception even though i am sending only one item as a parameter at a time and my stored procedure also accepts only one parameter.
C#:
private void UpdatePrice(List<ListItem> accounts)
{
var items = (from item in accounts
select item.itemNumbers);
con = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_updateItems";
con.Open();
foreach (var item in items)
{
cmd.Parameters.AddWithValue("@itemnumber", item.ToString());
cmd.Connection = con;
int n = cmd.ExecuteNonQuery();
if (n > 0)
Console.WriteLine("Price Updated Successfully");
else
Console.WriteLine("Failed");
}
con.Close();
//foreach(var item in items)
//{
// Console.WriteLine(string.Join(", ", item));
//}
Console.ReadKey();
}
My stored procedure is
C#:
create proc sp_updateItems
(
@itemnumber varchar(20)
)
as
begin
update station set Newprice = price*2
where itemnumber=@itemnumber
end