Stored procedure:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE spInsertData
@UNAME varchar(20),
@Pass varchar(20),
@Active bit,
@City nvarchar(20),
@Phone nvarchar(10)
AS
BEGIN
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO Login
VALUES(@UNAME, @Pass, @Active, @City, @Phone)
SELECT SCOPE_IDENTITY() AS Id
END
GO
C# code-behind:
namespace AjaxJqueryDemo
{
public partial class Reg : System.Web.UI.Page
{
TraineeEntities DB = new TraineeEntities();
protected void btnReg_Click(object sender, EventArgs e)
{
int i = DB.spInsertData(uname.Text, Pass.Text,true, city.Text, pHno.Text);
}
}
}
How to fetch the scope identity so that I can ensure data is inserted?
I am not using gridview and neither am I interested in using it. How can I fetch it simply?
Please give answers with more detailed explanation so that I can understand..
User(orTraineeor whatever) and would be using Entity Framework to actually insert it, you would have this feature (getting back the newly insertedId) automatically and for free - no effort on your part needed. Do you really need to use that stored procedure? Really?!?!?!SELECTing instead ofRETURNing? Chances are your ID is anINTso you don't need all the overhead of a data set being returned. If, again for some unknown reason, you mustSELECTit, then treat it in the same way you return any other data - as a set of columns and rows.