Data is Null. This method or property cannot be called on Null values Data is Null. This method or property cannot be called on Null values asp.net asp.net

Data is Null. This method or property cannot be called on Null values


You shouldn't be trying to convert the null values from the proc into ints - so before you create the MovieGenre instance you need to check the nullable fields using the SqlDataReader.IsDBNull method:

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.isdbnull.aspx

Assuming that the GenreID and MovieGenreID are nullable ints you could do something like:

movieGenre.Add(new MovieGenre {  MovieID = reader.GetInt32(movieIDIndex),  MovieGenreID = reader.IsDBNull(movieGenreIDIndex) ? null : reader.GetInt32(movieGenreIDIndex),  GenreID = reader.IsDBNull(genreIDIndex) ? null : reader.GetInt32(genreIDIndex)});


This error happens immediately after I enabled C# 8 nullable feature in my Entity Framework Core 3.1 project.

The solution is to change your entity properties to their nullable counterparts. For example,

Change from:

public class Person {  public int Id { get; set; }  public string Name { get;set; }  public string Address { get;set; }}

To:

public class Person {  public int Id { get; set; }  public string Name { get;set; }  public string? Address { get;set; }  //change address to nullable string since it is nullable in database}


Edit your select statement as follows to handle null issue.

SELECT ISNULL(m.MovieID,0) AS MovieID,        ISNULL(g.GenreID,0) AS GenreID,        ISNULL(mg.MovieGenreID,0) AS MovieGenreID,       ISNULL(g.Genre,'') AS GenreFROM --rest of your query...