Why can't I get the return value (Scope_Identity) by running my own stored procedure (SQL Server 2008) using this code below written in VB.NET 2008? SP inserts a new record into table1, but I have 0 as the return value!
What is wrong with him?
here is my vb.net code and my SP:
Public Function Insert(ByVal Obj As entity, connectionString As String) As Integer
Dim ScopeIdentity As Integer
Dim Connection As New SqlConnection(connectionString)
Using Command As New SqlCommand
With Command
.Connection = Connection
.CommandTimeout = 300
.CommandType = CommandType.StoredProcedure
.CommandText = "S_Test"
If .Connection.State <> ConnectionState.Open Then
.Connection.Open()
End If
SqlCommandBuilder.DeriveParameters(Command)
With .Parameters
.Item("@Name").Value = Obj.Name
.Item("@Age").Value = Obj.Age
End With
Dim ScopeIdentityParameter As New SqlParameter("ReturnValue", SqlDbType.Int)
ScopeIdentityParameter.Direction = ParameterDirection.ReturnValue
Command.Parameters.Add(ScopeIdentityParameter)
Command.ExecuteNonQuery()
ScopeIdentity = System.Convert.ToInt32(ScopeIdentityParameter.Value)
End With
End Using
Return ScopeIdentity
End Function
my own simple stored procedure structure:
insert into dbo.Table1(Name, Age) values(@Name, @Age)
return SCOPE_IDENTITY()
source
share