title: How to resolve an IndexOutOfRangeException on bit value?
body: <p>I'm reading back a bit value from an SQL table, and casting as a boolean value to map to a boolean model value.</p>
  
  <p>But when I read back the <code>IsPDLChecked</code> bit field and initialise as false, I get an <code>index out of range exception</code>.</p>
  
  <p>I looked up the definition of the exception and I'm not sure why the value is out of range due to it being <em>init with a false value</em>, ie, <code>0</code>. So it's not a negative range value.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>Why am I getting an <code>IndexOutOfRange exception</code> although the bit value being read back is cast to bool and init to false?</p>
  
  <p><strong>Code:</strong></p>
  
  <p>Model - </p>
  
  <pre><code>public partial class EmailContact
  {
      public int ContactID { get; set; }
      public bool IsPDLChecked { get; set; }
      public string ContactDisplayName { get; set; }
      public string ContactEmailAddress { get; set; }   
  
  }
  </code></pre>
  
  <p>Data reader snippet -</p>
  
  <pre><code>while (dataReader.Read())
  {
      statusList.Add(new EmailContact(dataReader["IsPDLChecked"] as bool? ?? false,dataReader["ContactDisplayName"].ToString(), dataReader["ContactEmailAddress"].ToString()));
  }
  </code></pre>
  
  <p><strong>Error detail:</strong></p>
  
  <blockquote>
    <p>System.IndexOutOfRangeException was caught<br>
    HResult=-2146233080<br>
    Message=IsPDLChecked<br>
    Source=System.Data<br>
    StackTrace:  </p>
    
    <p>at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName)<br>
    at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)<br>
    at System.Data.SqlClient.SqlDataReader.get_Item(String name)  </p>
  </blockquote>
  
  <p>DB Schema: (I did notice the "CHARACTER_MAX_LENGTH" column is set to null on this):</p>
  
  <p><a href="https://i.stack.imgur.com/2zBZi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2zBZi.png" alt="enter image description here"></a></p>
  
  <p>DB values: (IsPDLChecked has a value)</p>
  
  <p><a href="https://i.stack.imgur.com/cgidc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cgidc.png" alt="enter image description here"></a></p>