Tuesday, March 20, 2012

Retrieving ID after insert

Hi, I have a Stored procedure doind an INSERT which then returns @.@.Identity.

I have set this parameters direction to output, however, when i run it I get an error saying procedure is expecting this output parameter..

Not sure where I am going wrong...

Can someone please help with retrieving the ID after an insert. What is the correct code in .NET?

Many ThanksShow your SP, and how you call it. You can get info like htis back as an output parameter, or as a return code. Absent knowing what you are doing, it is impossible to help.|||This is just a suggestion.

First define the input parameters and assign its values.

'Define the output parameter
With cmd.Parameters.Add("@.outputField", SqlDbType.Int)
.Direction = ParameterDirection.Output
End With

'Read the result
Dim dr As SqlDataReader = cmd.ExecuteReader
Dim returnId As Integer
If dr.Read Then
returnId = dr("OutputField")
End If
dr.Close()

My cmd is the reference to the SqlCommand.
OutputField – Field which you want to return.

Hope this might give you some help.|||Use Scope_Identity() instead of @.@.Identity.

Also..

cmd.ExecuteNonReader()

returnID = cmd.Parameters("@.outputField").Value
cmd.Dipose()

No comments:

Post a Comment