I am attempting to call a stored procedure in SQL Server from a database first MVC Entity Framework project but the return values always returns -1.
I have tried MANY different ways to fix this and have been unsuccessful so far. I have listed what I believe to be my relevant code below. Let me know if more code is necessary. Thanks to anyone who can help!
This is just my test code to see if its working correctly.
public ActionResult Index()
{
Cust_Entities cse = new Cust_Entities();
if (cse.DoesCustomerNumberExist("1") == 1)
{
return RedirectToAction("Index", "Home");
}
else
{
return RedirectToAction("About", "Home");
}
}
public virtual int DoesCustomerNumberExist(string customer_Number)
{
var customer_NumberParameter = customer_Number != null ?
new ObjectParameter("Customer_Number", customer_Number) :
new ObjectParameter("Customer_Number", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("DoesCustomerNumberExist", customer_NumberParameter);
}
SQL Server stored procedure code:
ALTER PROCEDURE [dbo].[DoesCustomerNumberExist]
@Customer_Number VARCHAR(20)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Exists INT
IF EXISTS(SELECT Customer_Number
FROM business_Final.dbo.AR_Customer
WHERE Customer_Number = @Customer_Number)
BEGIN
SET @Exists = '1'
END
ELSE
BEGIN
SET @Exists = '0'
END
RETURN @Exists
END
@Existsis declared as anINT, you should NOT use single quotes (since you're dealing with a numeric value - not a string) when setting values - useSET @Exists = 1- no single quotes, no unnecessary conversion from string to int etc.