Resolved Procedure or function sp_updateItems has too many arguments specified.

Anonymous

Well-known member
Joined
Sep 29, 2020
Messages
94
Programming Experience
Beginner
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.

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
 
This is related to other thread Cannot implicitly convert type error
If you don't want groups why are you grouping?
yes this is related but I had fixed that issue. I am fetching a list of account number with their corresponding items using linq. I am fetching account number as it is to be used for a different purpose. I am passing that list to updateprice function. Now I only want to extract item numbers from that list and pass it as a parameter to Stored procedure.
 
Last edited:
You can use nested loop. For each group in groups, for each item in group.
Alternative is to flatten the groups with a SelectMany query.
 
Back
Top Bottom