Search code examples
c#sqlprocedure

Procedure or function Event_Update has too many arguments specified


I am getting an error message Procedure or function Event_Update has too many arguments specified. while trying to update table event with procedure function from c# code

procedure for update

create procedure Event_Update
@id int,
@image varchar(50),
@title varchar(255),
@description varchar(255),
@date date

as

begin
update event set image = @image,title=@title,description=@description,event_date=@date where id=@id
end

Event Table

create table event(
id int identity(1,1) primary key,
image varchar(50) not null,
title varchar(255) not null,
description varchar(255) not null,
event_date date not null
)

C# code

public void update(int id,string title, string image, string events, string date)
    {
        cmd.CommandText = "Event_Update";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", id);
        cmd.Parameters.AddWithValue("@title", title);
        cmd.Parameters.AddWithValue("@image", image);
        cmd.Parameters.AddWithValue("@description", events);
        cmd.Parameters.AddWithValue("@date", date);
        cmd.ExecuteNonQuery();
    }

Solution

  • cmd is global variable? clear cmd.Parameters in update method:

    public void update(int id,string title, string image, string events, string date)
    {
        cmd.Parameters.Clear();
        cmd.CommandText = "Event_Update";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", id);
        cmd.Parameters.AddWithValue("@title", title);
        cmd.Parameters.AddWithValue("@image", image);
        cmd.Parameters.AddWithValue("@description", events);
        cmd.Parameters.AddWithValue("@date", date);
        cmd.ExecuteNonQuery();
    }