Search code examples
c#sql-serverapiasp.net-corestored-procedures

Stored procedure returning an entry with null values (when it's not)


For some reason, when I try to perform a query (through an API) with the entry's id, I get access where it contains null attributes. But, if I perform a question using other attributes (besides ID), it has the correct values.

For example: when I perform (the actual stored procedure code):

CREATE PROCEDURE [dbo].[spClient_GetById]
    @Id int
AS
BEGIN
    SELECT *
    FROM dbo.Client
    WHERE Id = @Id;
END

I get the entry

{
    "id": 2,
    "firstName": null,
    "lastName": null,
    "email": null,
    "phoneNumber": null,
    "houseNum": "1212",
    "street": "Downtown",
    "state": "WA",
    "city": "Vancouver",
    "cost": 12000.0000,
    "status": "Started",
    "eta": 0,
    "startDate": null,
    "completeDate": null,
    "contractorID": 0
}

But when I perform the following query on the same database,

CREATE PROCEDURE [dbo].[spClient_GetByHouse]
    @HouseNum nvarchar(10),
    @Street nvarchar(50) = null
AS
    IF @Street IS NULL
    BEGIN
        SELECT *
        FROM dbo.Client
        WHERE HouseNum = @HouseNum;
    END
    ELSE
    BEGIN
        SELECT *
        FROM dbo.Client
        WHERE HouseNum = @HouseNum AND Street LIKE @Street;
    END

I get the following entry (same entry btw):

{
    "id": 2,
    "firstName": "This",
    "lastName": "That",
    "email": "[email protected]",
    "phoneNumber": "3603602334",
    "houseNum": "1212",
    "street": "Downtown",
    "state": "WA",
    "city": "Vancouver",
    "cost": 12000.0000,
    "status": "Started",
    "eta": 0,
    "startDate": null,
    "completeDate": null,
    "contractorID": 1
}

Additionally, if I perform the SQL code in SQL Server, it returns all the correct values. So, the problem is when I'm calling that stored procedure through an API it's returning null values when it should.

Also, the first name, last name, and phone numbers defined to not allow nulls in the schema

More info: here's the function of calling the query my ID stored procedure:

public async Task<ClientModel> GetProjectById(int id)
{
    var records = await _dataAccess.LoadData<ClientModel, dynamic>("dbo.spClient_GetById", new { Id = id },
                                                                        _connectionString.SqlConnectionName);
    return records.FirstOrDefault();
}

And here's the function for calling the query by other attributes:

public async Task<ClientModel> GetProjectByHouse(string house, string? street)
{
    var p = new { HouseNum = house, Street = street };
    var records = await _dataAccess.LoadData<ClientModel, dynamic>("dbo.spClient_GetByHouse", p,
                                                                        _connectionString.SqlConnectionName);
    return trimValues(records.FirstOrDefault());
    // return records.FirstOrDefault();
}

And here's the API function for calling either previous functions:

public async Task<IActionResult> Get(int? projectId, string? house, string? street)
{
    if (projectId != null)
    {
        var project = await _clientData.GetProjectById(projectId ?? default(int));

        if (project == null)
            return NotFound();

        return Ok(project);
    }
    else if (house != null)
    {
        if (house.Length < 2)
            return BadRequest();

        var project = await _clientData.GetProjectByHouse(house, street);

        if (project == null)
            return NotFound();

        return Ok(project);
    }
    else
    {
        return BadRequest();
    }
}

Solution

  • Not sure if this was what solved it (but the problem is fixed). I had to delete the old db and republish a new one.