Click here to Skip to main content
16,016,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have written following stored procedure:
in which there are 2 tables arts and artist.artistId is a foreign key in arts table.

create procedure spInsertArts(
     @artsId int out,
     @name varchar(50),
     @category varchar(50),
     @artistId int
     )
     as
          begin
              declare @artistId int
		set @artistId=@@IDENTITY
			
            if exists(select artistId from artist)
              insert into arts(name,category,artistId) values(@name,@category,@artistId) 
           end 


I am getting error as:
Procedure or function spInsertArts has too many arguments specified.

please help
Posted
Updated 23-Apr-14 5:07am
v3
Comments
thatraja 23-Apr-14 10:47am    
how did you call the stored procedure? Show that portion too
Member 10724668 23-Apr-14 10:50am    
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name", TextBox1.Text);
cmd.Parameters.AddWithValue("@category", DropDownList1.SelectedValue);
SqlParameter output = new SqlParameter();
output.ParameterName = "@artsId";
output.SqlDbType = System.Data.SqlDbType.Int;
output.Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add(output);
con.Open();
cmd.ExecuteNonQuery();

Um...You declare four parameters, but you only provide three in your call - and in the SP you try to declare a variable with the same name as one of the parameters: @artistId

I suspect that you didn't mean to include @artistId in the parameters list.
 
Share this answer
 
Comments
thatraja 23-Apr-14 11:11am    
aaarggg.....beat me to it. 5!
Member 10724668 23-Apr-14 11:17am    
I have removed @artistId from parameter list but still I am getting error
OriginalGriff 23-Apr-14 11:25am    
What happens if you execute it within SSMS?
Quote:
C#
cmd.CommandType = System.Data.CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@name", TextBox1.Text);
cmd.Parameters.AddWithValue("@category", DropDownList1.SelectedValue);

SqlParameter output = new SqlParameter();
output.ParameterName = "@artsId";
output.SqlDbType = System.Data.SqlDbType.Int;
output.Direction = System.Data.ParameterDirection.Output;

cmd.Parameters.Add(output);
con.Open();
cmd.ExecuteNonQuery();
You have not passed the parameter "@artistId" in your code. Please pass that and try.
 
Share this answer
 
Comments
Member 10724668 23-Apr-14 11:15am    
@artistId is a foreign key. And i want it to get value of latest artistId of artist table.
That you have already declared inside the procedure. So, no need to declare that as a parameter. Remove that from parameters in the Stored Procedure.
Member 10724668 23-Apr-14 11:23am    
yes i have removed it.But I am getting Same error
As I observe, you have made the procedure unnecessarily complex. Moreover, you are not using @artsId, but passing it as a parameter. The simplified version of your Stored Procedure would look like...

create procedure spInsertArts(
@name varchar(50),
@category varchar(50)
)
as
begin

if exists(select artistId from artist)
insert into arts(name, category, artistId) values(@name, @category, @@IDENTITY)
end

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900