I'm reading back a bit value from an SQL table, and casting as a boolean value to map to a boolean model value.
But when I read back the IsPDLChecked
bit field and initialise as false, I get an index out of range exception
.
I looked up the definition of the exception and I'm not sure why the value is out of range due to it being init with a false value, ie, 0
. So it's not a negative range value.
Question:
Why am I getting an IndexOutOfRange exception
although the bit value being read back is cast to bool and init to false?
Code:
Model -
public partial class EmailContact
{
public int ContactID { get; set; }
public bool IsPDLChecked { get; set; }
public string ContactDisplayName { get; set; }
public string ContactEmailAddress { get; set; }
}
Data reader snippet -
while (dataReader.Read())
{
statusList.Add(new EmailContact(dataReader["IsPDLChecked"] as bool? ?? false,dataReader["ContactDisplayName"].ToString(), dataReader["ContactEmailAddress"].ToString()));
}
Error detail:
System.IndexOutOfRangeException was caught
HResult=-2146233080
Message=IsPDLChecked
Source=System.Data
StackTrace:at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName)
at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)
at System.Data.SqlClient.SqlDataReader.get_Item(String name)
DB Schema: (I did notice the "CHARACTER_MAX_LENGTH" column is set to null on this):
DB values: (IsPDLChecked has a value)