Something really weird today.
When I try to call a stored procedure in SQL Server with the objectContext from Entity Framework, using the function ExecuteStoreCommand, it always tries to convert to 'string' value to 'int' .. I don't know why.
Even if I set the output as an NvarChar type.
My exception is
Failed converting the varchar value 'text' to int data type
Here is my C# code:
public virtual string GetCardCodeLinkedToAttestation(string atBuisnessCode)
{
// Input param - String
SqlParameter atBuisnessCodeParameter = new SqlParameter("atBuisnessCode", atBuisnessCode);
// The output param - String
SqlParameter retval = new SqlParameter("retval", SqlDbType.NVarChar,15);
retval.Direction = ParameterDirection.Output;
((IObjectContextAdapter)this).ObjectContext.ExecuteStoreCommand("exec @retval = sp_GetCardCodeLinkedToAttestation @atBuisnessCode", atBuisnessCodeParameter, retval);
return (string)retval.Value;
}
Here is my stored procedure in SQL Server (I explicitly return 'test' for be sure is a string ):
ALTER PROCEDURE [dbo].[sp_GetCardCodeLinkedToAttestation]
@atBuisnessCode nvarchar(9)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @retval varchar(15);
set @retval= '';
-- Insert statements for procedure here
SET @retval = (SELECT TOP 1 [CardCode]
FROM [SC-SAP01].[SWC_PROD].[dbo].[OCPR]
WHERE [U_Attestation] = @atBuisnessCode)
RETURN 'test';
END
GO
Exception :
Failed converting the varchar value 'text' to int data type
I tried to set the value without return it, and declare the output in my stored procedure, but the value returned is always NULL.


int. If you want a string, use anoutputparameter or place it in a result set - don't usereturn. Also, you should avoid usingsp_as a prefix when naming your stored procedures - it's reserved for Microsoft's System Procedures.