简体   繁体   中英

SQL Server is throwing an error: Data is Null. This method or property cannot be called on Null values

I have this situation, I'm trying to call a stored procedure to return a list of tasks, with the ProjectId null. I tested the stored procedure and it is working, but on the code not. What am I doing wrong?

The view:

@foreach (var item in Model.Tasks)
    {
        @if (item.ProjectId == null)
        {
            <tr data-toggle="RecEdit" data-url="@(Url.Action("Edit") + "/" + item.TaskId)">
                <td style="width: 20%">@item.Title</td>
                <td style="width: 20%">@item.Description</td>
            </tr>
        }

    }

This is the model class:

public class CreateTaskVM
{
    public Task task {get; set;}
    public ICollection<Task> Tasks { get; set; }
    public ICollection<Project> projects {get; set;}
    public ICollection<Employee> Employees { get; set; }
    public ICollection<ProjectStatus> StatusTypes { get; set; }
}

And this is my controller:

public IActionResult RecTask()
{
        CreateTaskVM mytasks = new CreateTaskVM();

        var param = new SqlParameter("@ProjectId", "Null");
        mytasks.Tasks = context.Task.FromSql($"EXEC [dbo].[uspGET_Tasks] @ProjectId", param).ToList();

        return View(mytasks);
}

Also I was trying to do it in Identity but for some reason the MV come null. I just want to present a list of tasks with the value of ProjectId is null.

Thanks in advance.

The SqlParameter contains the string "Null" .

If you want to send a null value to a stored procedure, try using DBNull.Value :

var param = new SqlParameter("@ProjectId", DBNull.Value);

您必须将属性“ projectId”设置为可为空的属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM