Get Return Value from Stored procedure in T-SQL and asp.net

Rashmi
Rashmi
Member
78 Points
24 Posts

I have create a procedure with output parameter and trying to execute in asp.net MVC. But unable to get value.

C# Snippet:

public long SomeMethod(long eventId,string userId)
{
long duplicatedEventId = 0;
var paramEventId = new SqlParameter("@EventId", eventId);
paramEventId.DbType = DbType.Int64;
var paramUserID = new SqlParameter("@UserID", userId);
var paramDuplicatedEventId = new SqlParameter("@DuplicatedEventId", DbType.Int64);
paramDuplicatedEventId.Direction = ParameterDirection.Output;
_factory.ContextFactory.GetContext().Database.ExecuteSqlCommand("EXEC DuplicateEvent @EventId, @UserId, @DuplicatedEventId", paramEventId, paramUserID, paramDuplicatedEventId);
if (!Int64.TryParse(paramDuplicatedEventId.Value.ToString(), out duplicatedEventId))
{
duplicatedEventId = 0;
}
return duplicatedEventId;
}

 

I am successfully execute in SQL Management Studio as:

declare @a bigint
exec DuplicateEvent 24968,'52f7cc30-9a99-4f29-8732-20660b8',@a output
print 'a='+convert(varchar,@a)

What I am missing?

Thanks

Views: 8128
Total Answered: 1
Total Marked As Answer: 0
Posted On: 25-Jan-2017 03:26

Share:   fb twitter linkedin
Answers
Smith
Smith
Participant
360 Points
110 Posts
     

I think you are missing OUTPUT keyword in command. Try following

_factory.ContextFactory.GetContext().Database.ExecuteSqlCommand("EXEC DuplicateEvent @EventId, @UserId, @DuplicatedEventId OUTPUT", paramEventId, paramUserID, paramDuplicatedEventId);
Posted On: 25-Jan-2017 03:30
banner

Blog

Active User (0)

No Active User!
 Log In to Chat