title: OleDbDataReader throws a NullReferenceException when data is loaded into a Datatable using the Load() method
body: <p>I have an application that needs to load data from an IBM DB2 database view into a <code>System.Data.DataTable</code> using the <code>Load(IDataReader reader)</code> function. However, when data is loaded with many thousand items the reader at some point throws a <code>NullReferenceException</code>.</p>
  <p>This appears to only happen on views, since loading data from a normal database table returns the data without an error.</p>
  <p>I would expect the data from the reader to be loaded into DataTable without a NullReferenceException since any nullable columns should simply be marked as nullable in the Datatable column.</p>
  <p>The timeout for the connection as well as for the command are set to absurd values, so the chance of the reader simply timing out should be eliminated.</p>
  <p>Connection string:</p>
  <pre><code>private const string connectionString = &quot;Provider=IBMDA400;Data Source=...;User ID=...;Password=...;Force Translate=37;Connect Timeout=600&quot;;
  </code></pre>
  <p>OleDbCommand object creation:</p>
  <pre><code>new OleDbCommand(query, Connection) { CommandType = CommandType.Text, CommandTimeout = 500 }.ExecuteReader();
  </code></pre>
  <p>The following picture shows the error occurring at the Load() method.</p>
  <p><a href="https://i.stack.imgur.com/mCtbY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mCtbY.png" alt="Code " /></a></p>
  <p>Here is the full codeblock:</p>
  <pre><code>        var dataTable = new DataTable();
          var reader = Oledb.executeQuery($&quot;SELECT * FROM ... WHERE ...&quot;);
          dataTable.Load(reader);
  </code></pre>
  <p>Oledb.executeQuerry code:</p>
  <pre><code>                return new OleDbCommand(query, Connection) { CommandType = CommandType.Text, CommandTimeout = CommandTimeout }.ExecuteReader();
  </code></pre>
  <p>Edit:</p>
  <p>Here is the full Stacktrace of the exception:</p>
  <pre><code>    &gt;System.Data.dll!System.Data.OleDb.OleDbDataReader.GetRowDataFromHandle()
  System.Data.dll!System.Data.OleDb.OleDbDataReader.GetValueBinding(System.Data.OleDb.MetaData info)
  System.Data.dll!System.Data.OleDb.OleDbDataReader.GetValues(object[] values)
  System.Data.dll!System.Data.ProviderBase.DataReaderContainer.CommonLanguageSubsetDataReader.GetValues(object[] values)
  System.Data.dll!System.Data.ProviderBase.SchemaMapping.LoadDataRow()
  System.Data.dll!System.Data.Common.DataAdapter.FillLoadDataRow(System.Data.ProviderBase.SchemaMapping mapping)
  System.Data.dll!System.Data.Common.DataAdapter.FillFromReader(System.Data.DataSet dataset, System.Data.DataTable datatable, string srcTable, System.Data.ProviderBase.DataReaderContainer dataReader, int startRecord, int maxRecords, System.Data.DataColumn parentChapterColumn, object parentChapterValue)
  System.Data.dll!System.Data.Common.DataAdapter.Fill(System.Data.DataTable[] dataTables, System.Data.IDataReader dataReader, int startRecord, int maxRecords)
  System.Data.dll!System.Data.Common.LoadAdapter.FillFromReader(System.Data.DataTable[] dataTables, System.Data.IDataReader dataReader, int startRecord, int maxRecords)
  System.Data.dll!System.Data.DataTable.Load(System.Data.IDataReader reader, System.Data.LoadOption loadOption, System.Data.FillErrorEventHandler errorHandler)
  System.Data.dll!System.Data.DataTable.Load(System.Data.IDataReader reader)
  &gt;ProductionRecords.dll!BbtExtensions.ProductionRecords.GetIsnrsForPon.AnonymousMethod__0() Line 205
  mscorlib.dll!System.Threading.Tasks.Task&lt;System.Threading.Tasks.Task&lt;System.Collections.Generic.List&lt;BbtExtensions.ProductionRecords.IsnrDto&gt;&gt;&gt;.InnerInvoke()
  mscorlib.dll!System.Threading.Tasks.Task.Execute()
  mscorlib.dll!System.Threading.Tasks.Task.ExecutionContextCallback(object obj)
  mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
  mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
  mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot)
  mscorlib.dll!System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution)
  mscorlib.dll!System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
  mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch()
  mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
  </code></pre>