I have the following stored procedure:
CREATE PROCEDURE dbo.sp_ins_test
@Title NVARCHAR (100) ,
@ExamId INT ,
@TopicId INT
AS
BEGIN
INSERT INTO dbo.Test
(
TestId,
ExamId,
TopicId
)
VALUES (
@TestId,
@ExamId,
@TopicId
)
Here's the code that calls the stored procedure:
List<SqlParameter> parameterList = new List<SqlParameter>();
parameterList.Add(new SqlParameter ("@Title", "Practice " + testNumber));
parameterList.Add(new SqlParameter ("@ExamId", examId == 0 ? null : examId));
parameterList.Add(new SqlParameter ("@TopicId", testTopicId == 0 ? null : testTopicId));
SqlParameter[] parameters = parameterList.ToArray();
int result = db.Database.ExecuteSqlCommand(sql, parameters);
My intention was that if the examId or the topicId were 0 then I want to pass a null to the sp_ins_test stored procedure. However I get an error saying:
Error 2
Type of conditional expression cannot be determined because there is no implicit
conversion between '<null>' and 'int'
for both the examId and testTopicId lines. Can anyone tell me how I can insert a null into these columns if the value of examId or testTopicId is 0 ?