Hello friends,
I am developing an application in MVC 4.0 using entity framework. When i insert a row in the database table, the value is inserted successfully. I am using scope_identity() to get the Identity value of the newly inserted column. When i execute the stored procedure, it is showing the Scope_identity() value correctly. But when I am trying to insert the value through front-end code using stored procedure, it is returning -1. Though the data is inserted successfully, it is returning -1. Please help me in solving this issue. I need to get the ID.
Jignesh TrivediPosted Sep 19, 2014, 4:29 AM
Hi,
using output parameter you can get identity column value....
Please refer
http://aspsnippets.com/Articles/Return-Identity-Auto-Increment-Column-value-after-record-insert-in-SQL-Server-Database-using-ADONet-with-C-and-VBNet.aspx
http://www.massimilianobianchi.info/articles/16/Create-a-Stored-Procedure-and-return-identity-value.aspx
hope this will help you.
krishna bharathPosted Sep 22, 2014, 3:32 AM
Thanks for the info. I have used the same method and I got the exact ID value. Thank you all.
Thanks,
Krishna Bharath
krishna bharathPosted Sep 19, 2014, 2:39 AM
This is how i am writing the stored procedure.
@UserId int,
@IP nvarchar(20)
The table tblData has the following columns
ID int PrimaryKey Identity
UserID int null
Date datetime null
IP nvarchar(20) null
insert into tblData values(@UserID,GETDATE(),@IP)
return scope_identity()
My Asp.NET code is
DataEntites obj=new DataEntites();
int a=obj.SPName(UserID,IP);
In this way am passing the values to the database.
The a value is displaying as -1.
But when am executing the stored procedure it is returning the appropriate value.
I don't know what mistake I am doing.
Ravi KumarPosted Sep 19, 2014, 2:29 AM