An SqlParameter with ParameterName '@IVRECORDID' is not contained by this SqlParameterCollection.

Hi

I keep getting the error in the title whenever I try to retun a parameter from a SQL insert procedure.

Please can you tell me what I'm missing. It keeps looking for the parameter IVRECORDID but it is supplied by the SP.

it worked fine inserting records but now I want to return an ID after insert.

cmd.Parameters("@IVRECORDID").Direction = ParameterDirection.Output

VB

       If Session("ModeType") = "Insert" Then


            Dim strConnString As String = ConfigurationManager.ConnectionStrings("IrisConnectionString").ConnectionString

            Dim con As New SqlConnection(strConnString)

            Dim cmd As New SqlCommand()

            cmd.CommandType = CommandType.StoredProcedure

            cmd.CommandText = "INSERT_IQA"



            cmd.Parameters.Add("@TRAINEEID", SqlDbType.NVarChar).Value = Request("TRAINEEID")

            cmd.Parameters.Add("@POT", SqlDbType.Int).Value = Request("POT")

            cmd.Parameters.Add("@QUALSCHEMEID", SqlDbType.Int).Value = ddlQualSchemeID.SelectedValue.Trim()

            cmd.Parameters.Add("@IVTYPEID", SqlDbType.Int).Value = ddlIVType.SelectedValue.Trim()

            cmd.Parameters.Add("@STAFFID", SqlDbType.NVarChar).Value = User.Identity.Name

            cmd.Parameters.Add("@DATEPLANNED", SqlDbType.Date).Value = txtDateCompleted.Text.Trim()

            cmd.Parameters.Add("@DATEACTUAL", SqlDbType.Date).Value = txtActualDate.Text.Trim()

            cmd.Parameters.Add("@DATECOMPLETED", SqlDbType.Date).Value = txtDateCompleted.Text.Trim()

            cmd.Parameters.Add("@IVOUTCOMEID", SqlDbType.Int).Value = ddlOutcome.SelectedValue.Trim()

            cmd.Parameters.Add("@IVSITEID", SqlDbType.Int).Value = ddlIQASite.SelectedValue.Trim()

            cmd.Parameters.Add("@ASSESSORID", SqlDbType.NVarChar).Value = ddlAssessorID.SelectedValue.Trim()


            cmd.Parameters("@IVRECORDID").Direction = ParameterDirection.Output


            con.Open()

            cmd.ExecuteNonQuery()


            con.Close()
            ' Hidden Value to Select Scope Identity value in Gridview
            hdIVRecordID.Value = cmd.Parameters("@IVRECORDID").Value.ToString()

            ' Bind the GridView
            GridIVRecs.DataBind()

Open in new window



SQL SP

ALTER PROCEDURE [dbo].[INSERT_IQA]

@IVRECORDID int output,
		 @TRAINEEID varchar(16),
           @POT int,
           @QUALSCHEMEID int,
           @IVTYPEID int,
           @STAFFID int,
           @DATEPLANNED datetime,
           @DATEACTUAL datetime,
           @DATECOMPLETED datetime,
           @IVOUTCOMEID int,
           @IVSITEID int,
           @ASSESSORID varchar(16)
AS

Begin
INSERT INTO I_TRAINEE_IV_RECORDS
           ([TRAINEEID]
           ,[POT]
           ,[QUALSCHEMEID]
           ,[IVTYPEID]
           ,[STAFFID]
           ,[DATEPLANNED]
           ,[DATEACTUAL]
           ,[DATECOMPLETED]
           ,[IVOUTCOMEID]
           ,[IVSITEID]
           ,[ASSESSORID])
     VALUES
           (@TRAINEEID
           ,@POT
           ,@QUALSCHEMEID
           ,@IVTYPEID
           ,@STAFFID
           ,@DATEPLANNED
           ,@DATEACTUAL
           ,@DATECOMPLETED
           ,@IVOUTCOMEID
           ,@IVSITEID
           ,@ASSESSORID)


--SET @IVRECORDID=SCOPE_IDENTITY()

--      RETURN  @IVRECORDID
      
end

Open in new window

EdAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

You need to UnComment the IVRECORDID in stored procedure for returning the output.

ALTER PROCEDURE [dbo].[INSERT_IQA]

          @IVRECORDID int output,
	       @TRAINEEID varchar(16),
           @POT int,
           @QUALSCHEMEID int,
           @IVTYPEID int,
           @STAFFID int,
           @DATEPLANNED datetime,
           @DATEACTUAL datetime,
           @DATECOMPLETED datetime,
           @IVOUTCOMEID int,
           @IVSITEID int,
           @ASSESSORID varchar(16)
AS

Begin

INSERT INTO I_TRAINEE_IV_RECORDS
           ([TRAINEEID]
           ,[POT]   
           ,[QUALSCHEMEID]
           ,[IVTYPEID]
           ,[STAFFID]
           ,[DATEPLANNED]
           ,[DATEACTUAL]
           ,[DATECOMPLETED]
           ,[IVOUTCOMEID]
           ,[IVSITEID]
           ,[ASSESSORID])
     VALUES
           (@TRAINEEID
           ,@POT
           ,@QUALSCHEMEID
           ,@IVTYPEID
           ,@STAFFID
           ,@DATEPLANNED
           ,@DATEACTUAL
           ,@DATECOMPLETED
           ,@IVOUTCOMEID
           ,@IVSITEID
           ,@ASSESSORID)


SET @IVRECORDID = SCOPE_IDENTITY()

RETURN  @IVRECORDID
      
end

Open in new window

0
EdAuthor Commented:
Thanks but I tried that before I commented it out. Tried it again, still the same.
0
Lokesh B RDeveloperCommented:
Hi,

You didn't add the parameter to the collection.

here are the changes.

1.  Add parameter to the collection

cmd.Parameters.Add("@IVRECORDID", SqlDbType.Int).Direction = ParameterDirection.Output

Open in new window


2. UnComment the Stored procedure line.

ALTER PROCEDURE [dbo].[INSERT_IQA]

          @IVRECORDID int output,
	       @TRAINEEID varchar(16),
           @POT int,
           @QUALSCHEMEID int,
           @IVTYPEID int,
           @STAFFID int,
           @DATEPLANNED datetime,
           @DATEACTUAL datetime,
           @DATECOMPLETED datetime,
           @IVOUTCOMEID int,
           @IVSITEID int,
           @ASSESSORID varchar(16)
AS

Begin

INSERT INTO I_TRAINEE_IV_RECORDS
           ([TRAINEEID]
           ,[POT]   
           ,[QUALSCHEMEID]
           ,[IVTYPEID]
           ,[STAFFID]
           ,[DATEPLANNED]
           ,[DATEACTUAL]
           ,[DATECOMPLETED]
           ,[IVOUTCOMEID]
           ,[IVSITEID]
           ,[ASSESSORID])
     VALUES
           (@TRAINEEID
           ,@POT
           ,@QUALSCHEMEID
           ,@IVTYPEID
           ,@STAFFID
           ,@DATEPLANNED
           ,@DATEACTUAL
           ,@DATECOMPLETED
           ,@IVOUTCOMEID
           ,@IVSITEID
           ,@ASSESSORID)


SET @IVRECORDID = SCOPE_IDENTITY()

RETURN  @IVRECORDID
      
end

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.