Hi,
I am trying to insert a new record in sql server using SP.
I need last_increment_id after insert command. but it produce error
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1.
Please help
Sql - SP
USE [db_my_database]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[data_add]
@ID int OUTPUT,
@TYPEID int = null OUTPUT,
@name nvarchar(50),
@code nvarchar(50),
@type nvarchar(50)
AS
BEGIN transaction
SET NOCOUNT ON
SET @TYPEID = (select id from category where name = @type)
insert into mytable(name, code, type)
values (@name, @code, @TYPEID)
SET @ID = IDENT_CURRENT('mytable')
RETURN @ID
commit transaction
if @@error <> 0
begin
ROLLBACK transaction
end
VB.Net Code -
Dim command As SqlCommand = New SqlCommand("data_add", conn)
command.CommandType = CommandType.StoredProcedure
command.Parameters.Add("@ID", SqlDbType.Int).Direction = ParameterDirection.Output
command.Parameters.Add("@name", SqlDbType.NVarChar).Value = "name here"
command.Parameters.Add("@code", SqlDbType.NVarChar).Value = "code here"
command.Parameters.Add("@type", SqlDbType.NVarChar).Value = "type here"
command.ExecuteNonQuery()