nazar tvm
nazar tvm

Reputation: 99

Error while calling stored procedure from front end

ALTER PROCEDURE [dbo].[spUpdateQuizScore]
-- Add the parameters for the stored procedure here
@Score INT=0,
@CorrectAnswer INT=0,
@WrongAnswer INT=0,
@TimeSpent INT=0,
@QuizId INT=0,
@UserId INT=0,
@UserName NVARCHAR(200)="No User",
@GroupId INT=0,
@GroupName NVARCHAR(200)="NoGroup"
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    INSERT INTO QuizScores(Score, CorrectAnswers, WrongAnswers, TimeSpent, QuizId, UserId,
       UserName, GroupId, GroupName) 
    VALUES(@Score, @CorrectAnswer, @WrongAnswer, @TimeSpent, @QuizId, @UserId,
       @UserName, @GroupId, @GroupName)
            END 

This is my stored procedure here i am inserting the data into the quiz scores

 public void UpdateUserQuizScore(int correctAnswer, int wrongAnswer, int score, int quizId, int userId,
            string userName, int groupId, string GroupName,int sid)
        {

            var dbCon = new DBConnection("Quiz");
            dbCon.AddParameter("@Score", score);
            dbCon.AddParameter("@CorrectAnswer", correctAnswer);
            dbCon.AddParameter("@WrongAnswer", wrongAnswer);
            dbCon.AddParameter("TimeSpent", 1);
            dbCon.AddParameter("@QuizId", quizId);
            dbCon.AddParameter("@SessionId", sid);
            if (userId != null && groupId == null)
            {
                dbCon.AddParameter("@UserId", userId);
                dbCon.AddParameter("@UserName", userName);
            }
            if (groupId != null && userId == null)
            {
                dbCon.AddParameter("@GroupId", userId);
                dbCon.AddParameter("@GroupName", userName);
            }
            if (groupId != null && userId != null)
            {
                dbCon.AddParameter("@UserId", userId);
                dbCon.AddParameter("@GroupId", groupId);
                dbCon.AddParameter("@UserName", userName);
                dbCon.AddParameter("@Groupname", GroupName);
            }
            dbCon.Execute_NonQuery("spUpdateQuizScore", null);
        }

This is my server side code at the time of execution i am getting this error--Procedure or function spUpdateQuizScore has too many arguments specified.

Upvotes: 1

Views: 51

Answers (2)

Rahul Tripathi
Rahul Tripathi

Reputation: 172378

The error message is quite clear. The number of input parameters which you are passing from the front end is greater than the expected number of parameters in your stored procedure.

Your stored procedure expects only these input parameters:

@Score INT=0,
@CorrectAnswer INT=0,
@WrongAnswer INT=0,
@TimeSpent INT=0,
@QuizId INT=0,
@UserId INT=0,
@UserName NVARCHAR(200)="No User",
@GroupId INT=0,
@GroupName NVARCHAR(200)="NoGroup"

For example you are passing:

dbCon.AddParameter("@SessionId", sid);

from front end and there is no parameter defined as @SessionId in your procedure.

Solution:

From the solution point of you, you need to alter your stored procedure and add the missing parameters which you are passing in your procedure. Something like this:

ALTER PROCEDURE [dbo].[spUpdateQuizScore]
 -- Add the parameters for the stored procedure here
@Score INT=0,
@CorrectAnswer INT=0,
@WrongAnswer INT=0,
@TimeSpent INT=0,
@QuizId INT=0,
@UserId INT=0,
@UserName NVARCHAR(200)="No User",
@GroupId INT=0,
@GroupName NVARCHAR(200)="NoGroup",
@SessionId INT=0

Upvotes: 3

RelatedRhymes
RelatedRhymes

Reputation: 428

Update your stored procedure.You missed on the SessionId here

ALTER PROCEDURE [dbo].[spUpdateQuizScore]
 -- Add the parameters for the stored procedure here
@Score INT=0,
@CorrectAnswer INT=0,
@WrongAnswer INT=0,
@TimeSpent INT=0,
@QuizId INT=0,
@UserId INT=0,
@UserName NVARCHAR(200)="No User",
@GroupId INT=0,
@GroupName NVARCHAR(200)="NoGroup",
@SessionId int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
INSERT INTO QuizScores(Score,CorrectAnswers,WrongAnswers,TimeSpent,QuizId,UserId,UserName,GroupId,GroupName,SessionId) VALUES(@Score,@CorrectAnswer,@WrongAnswer,@TimeSpent,@QuizId,@UserId,@UserName,@GroupId,@GroupName,@SessionId)
END

Upvotes: 1

Related Questions