title: How to use datareader with null values
body: <p>Say I have this class: </p>
  
  <pre><code>class myclass
  {
      public int Field1{ get; set; }
      public int? Field2 { get; set; } //Note Field2 is nullable
   }
  </code></pre>
  
  <p>I'm trying to populate a generic list with data coming from a database. As GetSqlInt32 implements INullable I would have thought that the below code would work. It doesn't. It generates an error if Field2 is null.             </p>
  
  <pre><code>List&lt;myclass&gt; mylist=new List&lt;myclass&gt;();
  
  int Field1_Ordinal = rdr.GetOrdinal("Field1");
  int Field2_Ordinal = rdr.GetOrdinal("Field2");
  
  SqlDataReader rdr = cmd.ExecuteReader(); //Execute a stored procedure to retrieve data from the database
  
  while (rdr.Read())
   {
     mylist.Add(new myclass
     {
        Field1 = rdr.GetSqlInt32(Field1_Ordinal).Value,
        Field2 = rdr.GetSqlInt32(Field2_Ordinal).Value  //Error if field2 is null
     });
   }
  </code></pre>
  
  <p>Any ideas why it doesn't work?</p>