简体   繁体   中英

Procedure or function has too many arguments specified

There are so many questions on SO on this exception. But none of them are useful to me.

Here is my Stored Procedure :

CREATE PROCEDURE HolidayStandardMappingInsert 
    @HolidayID bigint,
    @StandatdID smallint
AS
BEGIN
    INSERT INTO HolidayStandardMapping VALUES(@HolidayID,@StandatdID)
END
GO

And here is my Code:

int StdId = 0;
                SqlCommand cmdS = new SqlCommand("HolidayStandardMappingInsert", conn);
                cmdS.CommandType = CommandType.StoredProcedure;


                for (int i = 0; i < cblStandard.Items.Count; i++)
                {
                    if (cblStandard.Items[i].Selected == true)
                    {
                        if (StdId == 0)
                            StdId = Convert.ToInt32(cblStandard.Items[i].Value);
                        else
                            StdId = Convert.ToInt32(cblStandard.Items[i].Value);
                        cmdS.Parameters.AddWithValue("@HolidayID", NewRecordID);
                        cmdS.Parameters.AddWithValue("@StandatdID", StdId);
                        if (conn.State == ConnectionState.Open)
                        {
                            conn.Close();
                        }
                        conn.Open();
                        int res = cmdS.ExecuteNonQuery();
                        if (res > 0)
                        {

                        }
                    }
                }

Tell me what is missing?

You are using same SqlCommnad object for multiple insertions so previously added parameters are also present.

So either create a new SqlCommnad object inside loop or clear prevoius parameters.

This is how you can Clear Previously added parameters.

cmdS.Parameters.Clear();

You are adding parameters in a loop. So after second iteration, your command has 4 parameters.

Each time You are adding cmdS.Parameters.AddWithValue in a loop. So after the first iteration, it has already 2 parameters.

You need to clear the command parameters by cmdS.Parameters.Clear() before entering the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM