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

后端 未结 7 1036
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 20:07

I\'m working on an application where one can get information on movies from a database as well as add, update and delete the movies. In the database I have three tables (Movie,

相关标签:
7条回答
  • 2021-02-06 21:00

    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)
    });
    
    0 讨论(0)
提交回复
热议问题