Click here to Skip to main content
15,889,462 members
Articles / Web Development / ASP.NET
Tip/Trick

How to get output parameter values from stored procedure using Entity Framework.

Rate me:
Please Sign up or sign in to vote.
4.63/5 (10 votes)
13 Oct 2014CPOL 69.7K   12   9
This tip gives an explanation with example on how to get output parameter value from stored procedure using Entity Framework in ASP.Net

Introduction

This article helps the developers who works on Entity Framework how to get the output parameter values from stored procedures using Entity Framework in various scenarios like last inserted or updated record ID.

Background

I have already explained how to use stored procedures in Entity Framework in my previous post.

So this is a successor to my previous article, please refer here.

Using the code

1) First of all we need to add an output parameter to a procedure to get the value as mentioned in below example.

Ex :-

SQL
CREATE PROCEDURE [dbo].[SP_InsertTestData]
(
      @Value1                VARCHAR(20)
    , @Value2                VARCHAR(MAX)
    , @Value3                VARCHAR(50) = ''
    , @LastInsertedRecordID  INT OUTPUT
)
AS
BEGIN
**********
END

This procedure is created to insert one record to a table.

 

2) Now we need to set the value for this output parameter with last inserted record ID as below :-

SQL
--Inside the procedure body
--Below statement goes just after insert statement
SET @LastInsertedRecordID = SCOPE_IDENTITY()

3) Now we need to get this output parameter value in our code as below :-

SQL
using (this.objectContext = new TestEntities())
            {
                ObjectParameter objParam = new ObjectParameter("LastInsertedRecordID ", typeof(int));
                var i = this.asterixContext.UpdateFullyPaidRecordStatus(value1, value2, value3, objParam);
                this.objectContext.SaveChanges();
                return Convert.ToInt32(objParam.Value);
            }

 

 

Points of Interest

Hope this will benefit to those who started working with Entity Framework and want to use stored procedures with Entity Framework. Please contact me if any help you need from me.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
Having more than 9 years of development experience in various Microsoft Technologies like :-
ASP.Net, MVC, SQL Server, WCF, MS Commerce Server, Xamarin etc.

Also hands on experience on the client side coding with JavaScript/jQuery and framework like AngularJS. Worked with third party ASP.net controls like Telerik and DevExpress as well.

Very much interested in Microsoft technologies to explore and implement. Now started to helping coders who really need me.

My Blog


Microsoft Technology Master

Programming Communities


JS Fiddle | Stack Overflow

Awards


Microsoft MCTS Certification in ASP.Net 4.0

Comments and Discussions

 
QuestionUseful Article Pin
RondaKay6211-Sep-15 6:27
RondaKay6211-Sep-15 6:27 
QuestionRe: Useful Article Pin
SRS(The Coder)11-Sep-15 6:47
professionalSRS(The Coder)11-Sep-15 6:47 
SuggestionRe: Useful Article Pin
SRS(The Coder)11-Sep-15 6:53
professionalSRS(The Coder)11-Sep-15 6:53 
QuestionVoted 5 Pin
Peter Josefsson Sweden14-Oct-14 2:13
Peter Josefsson Sweden14-Oct-14 2:13 
GeneralMy vote of 3 Pin
aarif moh shaikh14-Oct-14 0:22
professionalaarif moh shaikh14-Oct-14 0:22 
GeneralMy vote of 2 Pin
Berk Kibarer13-Oct-14 23:11
Berk Kibarer13-Oct-14 23:11 
GeneralRe: My vote of 2 Pin
SRS(The Coder)13-Oct-14 23:15
professionalSRS(The Coder)13-Oct-14 23:15 
GeneralRe: My vote of 2 Pin
Berk Kibarer14-Oct-14 2:56
Berk Kibarer14-Oct-14 2:56 
GeneralRe: My vote of 2 Pin
SRS(The Coder)14-Oct-14 19:30
professionalSRS(The Coder)14-Oct-14 19:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.