• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

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

0
Ed
Asked:
Ed
  • 2
1 Solution
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now