1

I have a Stored Procedure and turns me an integer. I want to take and use that integer but get an error like:

'System.Data.Objects.ObjectResult1[System.Nullable1[System.Int32]]' type object couldn't assign to 'System.IConvertible'.

Here is my Stored Procedure:

CREATE PROC prDetectMurderer(@carId INT)      
AS BEGIN      
SET NOCOUNT ON      
SELECT TOP 1 DriverId FROM EventTable       
WHERE CarId = @carId    
AND Damage = 'false'      
ORDER BY EventId DESC      
SET NOCOUNT OFF      
END

And my c# code:

int sofor = Convert.ToInt16(entity.prDetectMurderer(11));

How can I solve it ?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • Did you import the stored procedure as a **Function**? See here:http://stackoverflow.com/questions/32140774/getting-data-from-stored-procedure-with-entity-framework – Salah Akbari Apr 07 '16 at 12:28
  • Yes I imported the sp as prDetectMurderer() method with an integer parameter – Batuhan Ozdal Apr 07 '16 at 12:29
  • And when you imported did you choose the return value of the procedure to be **Scalar**? – Salah Akbari Apr 07 '16 at 12:32
  • And also instead of `int` use `var` like this: `var sofor = Convert.ToInt16(entity.prDetectMurderer(11));` or use `System.Nullable` instead of `int`. – Salah Akbari Apr 07 '16 at 12:34
  • Based on the error, the Convert.ToInt16 method does not support the nullable type. The result is nullable because the stored procedure may not find any results that match. – momar Apr 07 '16 at 12:37

1 Answers1

0

This answer https://stackoverflow.com/a/35832669/3572241 shows how to get an int from a stored procedure, using EF.

Community
  • 1
  • 1
Peter Bill
  • 508
  • 3
  • 12