Harry
Harry

Reputation: 47

How to retrieve output parameter from stored procedure using Entity Framework 6

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..

Upvotes: 0

Views: 198

Answers (1)

Ankit Solanki
Ankit Solanki

Reputation: 36

Please check bellow code it will give you better suggestion.

ALTER PROCEDURE dbo.YourProcedure
@params VARCHAR(32),
@Table1ID INT = NULL OUTPUT
AS
BEGIN
  SET NOCOUNT ON;

  BEGIN TRANSACTION;

  INSERT dbo.Table1...
  SET @Table1ID = SCOPE_IDENTITY();
  INSERT dbo.Table2...

  COMMIT TRANSACTION;
END
GO

Upvotes: 2

Related Questions