Getting a "Procedure or function spNewPatient expects parameter @PXID (Identity Field) which was not supplied." when I try to insert data using dynamic SP and transactions. However, @PXID is an Identity field in tblPatient, so I didn't include it on the input parameters. Do I also have to begin a transaction here (since the stored procedure is technically a transaction)? Is that the reason why the @PXID is not generated by the stored procedure?
Code:Public SQLParams As New List(Of SqlParameter) Public Function ExecuteSP(SPName As String) As Integer Dim _ReturnVal As Integer Try SQLConn.Open() SQLCmd = New SqlCommand(SPName, SQLConn) SQLCmd.CommandType = CommandType.StoredProcedure For Each p As SqlParameter In SQLParams SQLCmd.Parameters.Add(p) SQLCmd.Parameters(p.ParameterName).Value = p.Value Next _ReturnVal = SQLCmd.ExecuteScalar() SQLConn.Close() Return _ReturnVal Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Encountered Critical Error") If SQLConn.State = ConnectionState.Open Then SQLConn.Close() Return -1 End Try FlushParams() End Function Public Sub AddParams(Name As String, Value As Object, Optional DataType As DbType = DbType.String) Dim newParams As New SqlParameter With {.ParameterName = Name, .Value = Value, .DbType = DataType} SQLParams.Add(newParams) End SubEdit: I changed the order of the fields in the stored procedure. It looks like none of the parameters I tried to pass from VB to SQL were transferred... I checked the list (SQLParams) and everything was in order.Code:USE [dbMacondo] ALTER PROC [dbo].[spNewPatient] @PXID int OUTPUT, <Input Variables> AS BEGIN BEGIN TRANSACTION BEGIN TRY INSERT INTO tblPatient (...) VALUES (...); SELECT @PXID = SCOPE_IDENTITY(); COMMIT TRANSACTION END TRY BEGIN CATCH IF XACT_STATE() <> 0 ROLLBACK TRANSACTION END CATCH END
Edit 2: Checked it again, and it's still looking for the @PXID Identity Field. Removed the @PXID in the stored procedure and everything went through nicely. However, how would I be able to implement this with using an output variable for scope_identity() in the stored procedure?




Reply With Quote