Hi, I try the following code in order to get the newly insert database record ID but always get a zero. Anybody has a clue what goes wrong? I can see the stored procedure does return a value if I run the SP only.
Private Function Insert() As Boolean
_PropertyID = Convert.ToInt32(SqlHelper.ExecuteScalar(System.Configuration.ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString, _
"Property_Insert", _
_LastUpdBy, _Addr1, _Addr2, _City, _ProvinceID, _PostalCode, _Owner))
Return _PropertyID > 0
End Function
ALTER PROCEDURE dbo.Property_Insert
(
@LastUpdBy INT,
@Addr1 NVARCHAR(50),
@Addr2 NVARCHAR(50),
@City NCHAR(20),
@ProvinceID INT,
@PostalCode NCHAR(10),
@Owner NVARCHAR(256)
)
AS
SET NOCOUNT ON
BEGIN
DECLARE @ErrorCode INT
SET @ErrorCode = 0
DECLARE @TranStarted BIT
SET @TranStarted = 0
IF( @@TRANCOUNT = 0 ) BEGIN
BEGIN TRANSACTION
SET @TranStarted = 1 END
ELSE
SET @TranStarted = 0
IF( @@ERROR <> 0 ) BEGIN
SET @ErrorCode = -1
GOTO Cleanup
END
IF EXISTS(SELECT PropertyID FROM Property WHERE Addr1=@Addr1 AND Addr2=@Addr2 AND Deleted=0)
BEGIN
SET @ErrorCode = 1
GOTO Cleanup
END
INSERT INTO Property
(
LastUpdBy,
Addr1,
Addr2,
City,
ProvinceID,
PostalCode,
Owner
)
VALUES
(
@LastUpdBy,
@Addr1,
@Addr2,
@City,
@ProvinceID,
@PostalCode,
@Owner
)
IF( @@ERROR <> 0 ) BEGIN
SET @ErrorCode = -1
GOTO Cleanup
END
IF( @TranStarted = 1 ) BEGIN
SET @TranStarted = 0
COMMIT TRANSACTION
END
RETURN @@IDENTITY
Cleanup:
IF( @TranStarted = 1 ) BEGIN
SET @TranStarted = 0
ROLLBACK TRANSACTION
END
RETURN @ErrorCode
END
Put a breakpoint on the Return and hit f11 once you reach the _PropertyID and see what value are you returning..
As far as I am concerent you have to define that in a method and then pass the values of the form to that method and why are you declaring the conn string here? You can make use of DataSets or LINQ and define the stored procedures and the tables in them
and it will your life much easier..
Please ask if I do not make any sense or completely misunderstood your issue..
Please remember to click “Mark as Answer” on the post that helps you.
Put a breakpoint on the Return and hit f11 once you reach the _PropertyID and see what value are you returning..
As far as I am concerent you have to define that in a method and then pass the values of the form to that method and why are you declaring the conn string here? You can make use of DataSets or LINQ and define the stored procedures and the tables in them
and it will your life much easier..
Please ask if I do not make any sense or completely misunderstood your issue..
Thanks for reply. I ran both VB and SP in debug mode. For VB, I got a 0 in _PropertyID whereas the SP ends at RETURN @@IDENTITY and it has the ID of newly added record.
DeepZone1
Member
32 Points
44 Posts
Get the record ID of newly insert record
Jun 12, 2012 08:12 PM|LINK
Hi, I try the following code in order to get the newly insert database record ID but always get a zero. Anybody has a clue what goes wrong? I can see the stored procedure does return a value if I run the SP only.
Private Function Insert() As Boolean _PropertyID = Convert.ToInt32(SqlHelper.ExecuteScalar(System.Configuration.ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString, _ "Property_Insert", _ _LastUpdBy, _Addr1, _Addr2, _City, _ProvinceID, _PostalCode, _Owner)) Return _PropertyID > 0 End FunctionALTER PROCEDURE dbo.Property_Insert ( @LastUpdBy INT, @Addr1 NVARCHAR(50), @Addr2 NVARCHAR(50), @City NCHAR(20), @ProvinceID INT, @PostalCode NCHAR(10), @Owner NVARCHAR(256) ) AS SET NOCOUNT ON BEGIN DECLARE @ErrorCode INT SET @ErrorCode = 0 DECLARE @TranStarted BIT SET @TranStarted = 0 IF( @@TRANCOUNT = 0 ) BEGIN BEGIN TRANSACTION SET @TranStarted = 1 END ELSE SET @TranStarted = 0 IF( @@ERROR <> 0 ) BEGIN SET @ErrorCode = -1 GOTO Cleanup END IF EXISTS(SELECT PropertyID FROM Property WHERE Addr1=@Addr1 AND Addr2=@Addr2 AND Deleted=0) BEGIN SET @ErrorCode = 1 GOTO Cleanup END INSERT INTO Property ( LastUpdBy, Addr1, Addr2, City, ProvinceID, PostalCode, Owner ) VALUES ( @LastUpdBy, @Addr1, @Addr2, @City, @ProvinceID, @PostalCode, @Owner ) IF( @@ERROR <> 0 ) BEGIN SET @ErrorCode = -1 GOTO Cleanup END IF( @TranStarted = 1 ) BEGIN SET @TranStarted = 0 COMMIT TRANSACTION END RETURN @@IDENTITY Cleanup: IF( @TranStarted = 1 ) BEGIN SET @TranStarted = 0 ROLLBACK TRANSACTION END RETURN @ErrorCode ENDSparkers
Contributor
2086 Points
470 Posts
Re: Get the record ID of newly insert record
Jun 12, 2012 08:24 PM|LINK
Did you try to debug?
Put a breakpoint on the Return and hit f11 once you reach the _PropertyID and see what value are you returning..
As far as I am concerent you have to define that in a method and then pass the values of the form to that method and why are you declaring the conn string here? You can make use of DataSets or LINQ and define the stored procedures and the tables in them and it will your life much easier..
Please ask if I do not make any sense or completely misunderstood your issue..
DeepZone1
Member
32 Points
44 Posts
Re: Get the record ID of newly insert record
Jun 12, 2012 08:28 PM|LINK
Sparkers
Contributor
2086 Points
470 Posts
Re: Get the record ID of newly insert record
Jun 12, 2012 08:34 PM|LINK
Lets do a Try Catch block here to get where its erroring out..
Can visit the following link to get the syntax for the try catch block..
http://msdn.microsoft.com/en-us/library/xtd0s8kd.aspx
hans_v
All-Star
35986 Points
6550 Posts
Re: Get the record ID of newly insert record
Jun 12, 2012 08:44 PM|LINK
First of all, why do you ask this in the Access Database Forum, instead of the SQL forum?
And to answer your question, read this:
http://www.mikesdotnetting.com/Article/54/Getting-the-identity-of-the-most-recently-added-record
(the second chapter)
DeepZone1
Member
32 Points
44 Posts
Re: Get the record ID of newly insert record
Jun 12, 2012 08:57 PM|LINK
Sorry, I though this forum is for data access.
Im a bit confused. Does it mean that I need to use output parameter in the stored procedure in order to get the ID back?
Sparkers
Contributor
2086 Points
470 Posts
Re: Get the record ID of newly insert record
Jun 12, 2012 09:00 PM|LINK
Yes.. and set teh output paramenter to return an ID back and also return the value of Return from the stored proc..
DeepZone1
Member
32 Points
44 Posts
Re: Get the record ID of newly insert record
Jun 12, 2012 09:05 PM|LINK
Actualy I missed part of the context of the link.
Instead of using RETURN @@IDENTITY in the SP, I use SELECT SCOPE_IDENTITY() and it does return the ID.
Sparkers
Contributor
2086 Points
470 Posts
Re: Get the record ID of newly insert record
Jun 12, 2012 09:11 PM|LINK
http://www.codeproject.com/Articles/17667/Insert-and-retrieve-data-through-stored-procedure
http://asmaqureshi.blogspot.com/2010/05/use-stored-procedure-to-insert-data.html
http://www.aspnettutorials.com/tutorials/database/storedprocedure-vb.aspx
http://stackoverflow.com/questions/4766864/vb-net-why-i-can-not-get-the-return-value-from-my-own-stored-procedure
http://social.msdn.microsoft.com/forums/en-us/vbgeneral/thread/98F35D7C-E6D6-46A8-87E6-C03B7B6C75F4
Hope they help