I've got the following MS SQL function:
CREATE FUNCTION [dbo].[fn_NumApplications] ()
RETURNS int
AS
BEGIN
DECLARE @numRecords int = 0
SELECT @numRecords = COUNT(A.id) FROM Applications A
RETURN @numRecords
END
and the following C# code to call the function:
using (SqlConnection conn = new SqlConnection(mydbconnstring))
{
using (SqlCommand cmd = new SqlCommand("dbo.fn_numApplications", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
//execute
conn.Open();
string retVal = cmd.ExecuteScalar().ToString();
conn.Close();
}
}
When the function gets to cmd.ExecuteScalar(), I am getting a NullReferenceException.
I have tried calling the function with, and without "dbo.". I have also tried calling it using cmd.ExecuteReader() without success.
What's going wrong here?