title: dataReader.GetValue(i) throws System.InvalidOperationException
body: <p><strong>.Net 4.5 / WPF / MVVM / ADO.Net / Oracle 11g / Oracle.DataAccess.Client</strong></p>
  
  <p><strong>Using</strong> : <a href="https://msdn.microsoft.com/en-us/library/system.data.idatareader%28v=vs.110%29.aspx" rel="nofollow"><code>IDataReader</code></a> Interface</p>
  
  <hr>
  
  <p>So, I have <code>sealed</code> BusinessObject builder class, which takes in a <code>DataReader</code> object, converts the data inside each field in that DataReader based on PropertyMap - the <code>PropertyMap</code> is retrieved from a class - and returns a <code>ListItem</code>.</p>
  
  <p>This ListItem is then added to a List and the List is then used to process the data, display the data etc on the View.</p>
  
  <p>The whole process is working perfect with in other places inside my application and today I was trying to use the BusinessObject builder process to convert a new set of records to a List and use it in my DashBoard.</p>
  
  <p>The propertymap for this entiry is as below.</p>
  
  <pre><code>-       pinfo   Count = 9   System.Collections.Generic.Dictionary&lt;string,System.Reflection.PropertyInfo&gt;
      +       [0] {[LATEST_LOAD_TIME, System.DateTime LATEST_LOAD_TIME]}  
      +       [1] {[LAST_DELIVERY, System.DateTime LAST_DELIVERY]}    
      +       [2] {[MAXVALDATE, System.DateTime MAXVALDATE]}  
      +       [3] {[MINVALDATE, System.DateTime MINVALDATE]}  
      +       [4] {[COMPLETED, Int32 COMPLETED]}  
      +       [5] {[FAILED, Int32 FAILED]}    
      +       [6] {[IN_PROGRESS, Int32 IN_PROGRESS]}  
      +       [7] {[NOT_STARTED, Int32 NOT_STARTED]}  
      +       [8] {[TOTAL, Int32 TOTAL]}
  </code></pre>
  
  <p>But, as soon as the process hits the <code>if ()</code> clause below</p>
  
  <pre><code>for (int i = 0; i &lt; dataReader.FieldCount; i++)
  {
      if (dataReader.GetValue(i) != DBNull.Value)
      {
          try
          {   
  </code></pre>
  
  <p>it throws an <strong>System.InvalidOperationException</strong> exception. See the full exception details below.</p>
  
  <pre><code>+       dataReader  {Oracle.DataAccess.Client.OracleDataReader} System.Data.IDataReader {Oracle.DataAccess.Client.OracleDataReader}
  -       dataReader.GetValue(i)  'dataReader.GetValue(i)' threw an exception of type 'System.InvalidOperationException'  object {System.InvalidOperationException}
      -           base    {"Operation is not valid due to the current state of the object."}  System.SystemException {System.InvalidOperationException}
          -       base    {"Operation is not valid due to the current state of the object."}  System.Exception {System.InvalidOperationException}
              +       Data    {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
                          HelpLink    null    string
                          HResult -2146233079 int
              +       InnerException  null    System.Exception
                          Message "Operation is not valid due to the current state of the object."    string
                          Source  "Oracle.DataAccess" string
                          StackTrace  "   at Oracle.DataAccess.Client.OracleDataReader.GetValue(Int32 i)" string
              -       TargetSite  {System.Object GetValue(Int32)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
              +       [System.Reflection.RuntimeMethodInfo]   {System.Object GetValue(Int32)} System.Reflection.RuntimeMethodInfo
              -       base    {System.Object GetValue(Int32)} System.Reflection.MemberInfo {System.Reflection.RuntimeMethodInfo}
              +       CustomAttributes    Count = 0   System.Collections.Generic.IEnumerable&lt;System.Reflection.CustomAttributeData&gt; {System.Collections.ObjectModel.ReadOnlyCollection&lt;System.Reflection.CustomAttributeData&gt;}
              +       DeclaringType   {Name = "OracleDataReader" FullName = "Oracle.DataAccess.Client.OracleDataReader"}  System.Type {System.RuntimeType}
                          MemberType  Method  System.Reflection.MemberTypes
                          MetadataToken   100665607   int
              +       Module  {Oracle.DataAccess.dll} System.Reflection.Module {System.Reflection.RuntimeModule}
                      Name    "GetValue"  string
              +       ReflectedType   {Name = "OracleDataReader" FullName = "Oracle.DataAccess.Client.OracleDataReader"}  System.Type {System.RuntimeType}
                          Attributes  FamANDAssem | Family | Virtual | HideBySig  System.Reflection.MethodAttributes
                          CallingConvention   Standard | HasThis  System.Reflection.CallingConventions
                          ContainsGenericParameters   false   bool
                          IsAbstract  false   bool
                          IsAssembly  false   bool
                          IsConstructor   false   bool
                          IsFamily    false   bool
                          IsFamilyAndAssembly false   bool
                          IsFamilyOrAssembly  false   bool
                          IsFinal false   bool
                          IsGenericMethod false   bool
                          IsGenericMethodDefinition   false   bool
                          IsHideBySig true    bool
                          IsPrivate   false   bool
                          IsPublic    true    bool
                          IsSecurityCritical  true    bool
                          IsSecuritySafeCritical  true    bool
                          IsSecurityTransparent   false   bool
                          IsSpecialName   false   bool
                          IsStatic    false   bool
                          IsVirtual   true    bool
              +       MethodHandle    {System.RuntimeMethodHandle}    System.RuntimeMethodHandle
                          MethodImplementationFlags   IL  System.Reflection.MethodImplAttributes
              +       Non-Public members      
              +       Static members      
              +       Non-Public members  
  </code></pre>
  
  <p>I know the exception says </p>
  
  <blockquote>
    <p>Message   "Operation is not valid due to the current state of the
    object."</p>
  </blockquote>
  
  <p>and I thought the DataReader might be closed, but it is not. See below the status</p>
  
  <pre><code>-       dataReader  {Oracle.DataAccess.Client.OracleDataReader} System.Data.IDataReader {Oracle.DataAccess.Client.OracleDataReader}
      +       [Oracle.DataAccess.Client.OracleDataReader] {Oracle.DataAccess.Client.OracleDataReader} Oracle.DataAccess.Client.OracleDataReader
              Depth   0   int
              IsClosed    false   bool
              RecordsAffected -1  int
      -       Results View    Expanding the Results View will enumerate the IEnumerable   
          -       [0] {System.Data.Common.DataRecordInternal} object {System.Data.Common.DataRecordInternal}
          -       base    {System.Data.Common.DataRecordInternal} System.Data.Common.DbDataRecord {System.Data.Common.DataRecordInternal}
                  FieldCount  9   int
                  FieldCount  9   int
  </code></pre>
  
  <hr>
  
  <p>Also, please see that the DataReader record does contain values</p>
  
  <pre><code>-       _values {object[9]} object[]
                  [0] 0   object {decimal}
                  [1] 0   object {decimal}
                  [2] 0   object {decimal}
                  [3] 0   object {decimal}
                  [4] 1502    object {decimal}
          +       [5] {}  object {System.DBNull}
          +       [6] {}  object {System.DBNull}
          +       [7] {5/5/2015 12:00:00 AM}  object {System.DateTime}
          +       [8] {4/30/2015 12:00:00 AM} object {System.DateTime}
          +       [1] {System.Data.Common.DataRecordInternal} object {System.Data.Common.DataRecordInternal}
          +       [2] {System.Data.Common.DataRecordInternal} object {System.Data.Common.DataRecordInternal}
  </code></pre>
  
  <p>Initially the first item in the data record was of type <code>DateTime</code> and I thought the error might be because of that. So I changed the column ordinal to put a record of type <code>int</code> as the first item - but the exception is still thrown.</p>
  
  <p>I think it has to do something with the <code>GetValue()</code> method, but not sure.</p>
  
  <p>Anyone have any idea what possibly might be the issue and any advice / help?</p>
  
  <p><strong>Sorry for the long code / exception samples, but don't want to miss out any details...</strong></p>