You must explicitly check if a value of type DBNull
while (reader.Read()) { int y = (!reader["PublicationYear"] is DBNull) ? Convert.ToInt32(reader["PublicationYear"]) : 0; ... }
In fact, you can do this comparison by value as well as type:
reader["PublicationYear"] != DBNull.Value
In short, you can expect DBNull
be returned for zeros from the database, not null.
source share