Home > Mobile >  How to show data is null in ASP.NET Web API when database having null values?
How to show data is null in ASP.NET Web API when database having null values?

Time:03-29

I am calling my database from my ASP.Net Web API as

 [HttpGet]
    public  IActionResult GetOutput(int id)
    {
        string CheckJob = "exec sp_GetJob "  "@id= "   id;

        var result = _context.Job.FromSqlRaw(CheckJob).ToList().FirstOrDefault().Job;
        if(result == null){
               -- do other functions
        }
       return Ok(result);
    }

And My class is

 public class Job
    {
     public int Job {get;set;}
    }

And If for particular input, if value is not available then it should store null value in my variable result. But It is showing error as

System.Data.SqlTypes.SqlNullValueException: 'Data is Null. This method or property cannot be called on Null values.'

Stored Procedure

CREATE PROCEDURE sp_GetJob @id
AS
DECLARE @JobID AS int
BEGIN
set @JobID = ( select JobId from Config 
                  where id = @id)

SELECT @JobID AS Job
END

Can someone tell me if, If database is return null value then how to store null value in variable so that I can compare that variable to go forward ?

Thank you

CodePudding user response:

You can try to use the nullable value type.

public class Job
{
    public int? Job {get;set;}
}
  • Related