Too many arguments specified

I am working on an application that allows you to list movies as well as edit, insert and delete them. In the db I have three tables (Movie, Genre and MovieGenre) and this problem is related to adding the genre to the movie (adding to the MovieGenre table which stores the tracks of the movie genres).

Below is the relevant code as well as the exception. I just can't figure out why I am getting this error message because I am using the correct number of arguments as I can see it in C # code when sproc is called.

Can anyone see what the problem is?

Exception: The procedure or function usp_InsertMovieGenre contains too many arguments.

Exception Details: System.Data.SqlClient.SqlException: The procedure or function usp_InsertMovieGenre contains too many arguments.

C # code:

public void InsertMovieGenre(MovieGenre movieGenre) {

    using (SqlConnection conn = CreateConnection()) {
        try {
            SqlCommand cmd = new SqlCommand("dbo.usp_InsertMovieGenre", conn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@MovieID", SqlDbType.Int, 4).Value = movieGenre.MovieID;
            cmd.Parameters.Add("@GenreID", SqlDbType.Int, 4).Value = movieGenre.MovieGenreID;

            cmd.Parameters.Add("@MovieGenreID", SqlDbType.Int, 4).Direction = ParameterDirection.Output;

            conn.Open();

            cmd.ExecuteNonQuery();

            movieGenre.MovieID = (int)cmd.Parameters["@MovieGenreID"].Value;
        }
        catch {
        }
    }
}

      

sproc:

ALTER PROCEDURE usp_InsertMovieGenre
@GenreID varchar(500),
@MovieID int
AS
BEGIN
    INSERT INTO MovieGenre (GenreID, MovieID)
    VALUES (@GenreID, @MovieID);
END
GO

      

+3


source to share


3 answers


Error because of this line

cmd.Parameters.Add("@MovieGenreID", SqlDbType.Int, 4).Direction = ParameterDirection.Output; 

      

to allow changing the stored procdeurre



sproc:

ALTER PROCEDURE usp_InsertMovieGenre
    @GenreID varchar(500),
    @MovieID int,
    @MovieGenreID int output

    AS
    BEGIN
        INSERT INTO MovieGenre (GenreID, MovieID)
        VALUES (@GenreID, @MovieID);
     SELECT @MovieGenreID = @@identity   
    END
    GO

      

+7


source


You must add this output parameter to your stored procedure:

ALTER PROCEDURE usp_InsertMovieGenre
@GenreID varchar(500),
@MovieID int,
@MovieGenreID int OUTPUT
AS
BEGIN
    INSERT INTO MovieGenre (GenreID, MovieID)
    VALUES (@GenreID, @MovieID);
    SELECT @MovieGenreID = SCOPE_IDENTITY();
END
GO

      



+2


source


Remove this line. Your Sproc only accepts the first two parameters

cmd.Parameters
  .Add("@MovieGenreID", SqlDbType.Int, 4)
  .Direction = ParameterDirection.Output;

      

edit: to get the generated id you can use newId () I believe. This looks like your intention.

+1


source







All Articles