1

I have a below stored procedure with output parameter.

ALTER proc [dbo].[CRS_GetNewMessageCount]
@CaseId int,
@Type varchar(50),
@Location varchar(50),
@Count int out
as

begin
if @location='admin'
    begin
    if @type='consumer'
    select @Count=(Select count(fdId) from tb_CRS_Messages where fdCaseId=@CaseId and fdIsConsumerType=1 and fdIsReadatAdmin=0)
    else
    select @Count=(Select count(fdId) from tb_CRS_Messages where fdCaseId=@CaseId and fdIsMemberType=1 and fdIsReadatAdmin=0)
    end
else
begin
    if @type='consumer'
    select @Count=(Select count(fdId) from tb_CRS_Messages where fdCaseId=@CaseId and fdIsConsumerType=1 and fdIsReadatFront=0)
else
select @Count=(Select count(fdId) from tb_CRS_Messages where fdCaseId=@CaseId and fdIsMemberType=1 and fdIsReadatFront=0)
END
SELECT @Count
END

It is perfect working at SQL server see below output :

I am calling this stored procedure through Entity Framework :

using (DbContext db = new DbContext())
            {
                try
                {
                    var NewMessage = new ObjectParameter("Count", typeof(int));
                    int returnValue = 0;
                    db.CRS_GetNewMessageCount(CaseId, type, location, NewMessage);
                    int.TryParse(NewMessage.Value.ToString(), out returnValue);
                    return returnValue;
                }
                catch { return 0; }
            }

It gives null value in output parameter.

Please help.

4
  • and it works in sql management studio? Commented Apr 27, 2016 at 11:54
  • Using SELECT @Count is redundant. You don't capture the value of the out parameter because you enter null. Commented Apr 27, 2016 at 13:43
  • It solved using FirstOrDefault() next to the Stored Procedure. Thank you guys for your help. Commented Apr 27, 2016 at 14:14
  • duplicate Commented Jun 3, 2018 at 12:39

2 Answers 2

1

Not sure whether it makes a difference, but did you try to use "typeof(Int32)" instead of "typeof(int)"? Also try NewMessage as ObjectParameter and not as var, maybe it makes a difference.

ObjectParameter NewMessage = new ObjectParameter("NewMessage ", typeof(Int32));

Did you try to run your stored procedure without parameters, means return @Count-variable with a value no matter what parameters you enter? Like this you can identify whether your input parameters are wrong (handed over by C#) or not.

Sign up to request clarification or add additional context in comments.

Comments

-1

Hope this helps How to: Execute a Query Using a Stored Procedure with In and Out Parameters

https://msdn.microsoft.com/en-us/library/bb896334(v=vs.100).aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.