Idéalement, j'essaie d'obtenir une procédure stockée pour renvoyer 1 si existe ou 0 sinon.Procédure stockée SQL Server si elle existe
Ceci est la procédure stockée:
CREATE PROCEDURE [dbo].[spCheckForExistingTimecard]
@userId int,
@paYPeriodId int,
@exists bit = 0 OUTPUT
AS
BEGIN
IF EXISTS (SELECT COUNT (t.TimeCardId)
FROM TimeCard AS t
WHERE t.PayPeriodId = @payPeriodId
AND t.UserId = @userId)
RETURN 1
ELSE
RETURN 0
Voici le code appelant la procédure stockée:
public static int CheckForExistingTimecard(int userId, int payPeriodId)
{
using (SqlConnection connection = new SqlConnection(dbMaintenanceConnectionString))
{
connection.Open();
using (SqlCommand sqlCommand = new SqlCommand("spCheckForExistingTimecard", connection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("@userId", userId);
sqlCommand.Parameters.AddWithValue("@payPeriodId", payPeriodId);
return (int)sqlCommand.ExecuteScalar();
}
}
}
Le problème est que je reçois une erreur
référence d'objet non définie à une instance d'un objet
sur la ligne de retour du code appelant.
Toute aide serait grandement appréciée