title: Having weird exception using MAX(CASE WHEN) in query: DB2.iSeries.iDB2DCFunctionErrorException: 'An unexpected exception occurred
body: <p>I have the following query to put some data from rows in columns:</p>
  <pre><code>string qry3 =
              &quot;SELECT TABLENAME1.SLONO AS ORDER, TABLENAME1.SLLNNO AS LINE &quot; +
              &quot;,  MAX(CASE WHEN TABLENAME2.CZVRNM in ('SLEEVEDEPTH', 'LENGTH') THEN CAST(RTRIM(TABLENAME2.CZREFD) AS FLOAT(53)) END) AS SLEEVE &quot; +
              &quot;,  MAX(CASE WHEN TABLENAME2.CZVRNM in ('INSTALLATION') THEN CAST(RTRIM(TABLENAME2.CZREFD) AS FLOAT(53)) END) AS DAMPER_AI &quot; +
              &quot;FROM LOCATION.LOCATION2.TABLENAME1 TABLENAME1 LEFT JOIN LOCATION.LOCATION2.TABLENAME2 TABLENAME2 &quot; +
              &quot;     ON (TABLENAME1.SLONO = TABLENAME2.SPONO AND TABLENAME1.SLLNNO = TABLENAME2.SPLNNO)  &quot; +
              &quot;GROUP BY TABLENAME1.SLONO, TABLENAME1.SLLNNO &quot;;
  </code></pre>
  <p>But when I try to run it in the following code:</p>
  <pre><code>        try
          {
              conAS400.Open();
  
              iDB2Command command = conAS400.CreateCommand();
              command.CommandText = qry3;
  
              command.CommandTimeout = 0;
  
              iDB2DataReader reader = command.ExecuteReader();
              dtAS400 = new DataTable();
              dtAS400.Load(reader); //&lt;-- The exception occurs here. I can't even catch it
  
              conAS400.Close();
  
              retrieved = true;
          }
  
          catch (Exception ex)
          {
              MessageBox.Show(&quot;Could not retrive the information. Exception: &quot; + ex.Message);
  
              retrieved = false;
          }
          finally
          {
              conAS400.Close();
          }
  </code></pre>
  <p>I get this exception:</p>
  <blockquote>
  <p>&quot;IBM.Data.DB2.iSeries.iDB2DCFunctionErrorException: 'An unexpected
  exception occurred.  Type: System.AccessViolationException, Message:
  Attempted to read or write protected memory. This is often an
  indication that other memory is corrupt..'&quot;</p>
  </blockquote>
  <p>I have other queries that work just fine, but this one does not. I even tried the query on SQL as follows (credit to Gordon Linoff for providing this code):</p>
  <pre><code>SELECT 
      A.IDA, A.SomeInfo,
      MAX(CASE WHEN VarName in ('Depth', 'Depth2') THEN VarValue END) AS Depth,
      MAX(CASE WHEN VarName in ('Length') THEN VarValue END) AS Length
  FROM 
      A
  LEFT JOIN 
      B ON A.IDA = B.IDA
  GROUP BY 
      A.IDA, A.SomeInfo
  </code></pre>
  <p>And it works. What can I do?</p>
  <p>Drivers:</p>
  <p>Client Access ODBC Driver (32-bit) 12.00.00.00</p>
  <p>iSeries Access ODBC Driver 12.00.00.00</p>
  <p>EDIT: Here's the sql query without being a string.</p>
  <pre><code>SELECT TABLENAME1.SLONO AS ORDER, TABLENAME1.SLLNNO AS LINE, 
      MAX(CASE WHEN TABLENAME2.CZVRNM in ('SLEEVEDEPTH', 'LENGTH') THEN CAST(RTRIM(TABLENAME2.CZREFD) AS FLOAT(53)) END) AS SLEEVE, 
      MAX(CASE WHEN TABLENAME2.CZVRNM in ('INSTALLATION') THEN CAST(RTRIM(TABLENAME2.CZREFD) AS FLOAT(53)) END) AS DAMPER_AI 
  FROM LOCATION.LOCATION2.TABLENAME1 TABLENAME1 LEFT JOIN LOCATION.LOCATION2.TABLENAME2 TABLENAME2 
      ON (TABLENAME1.SLONO = TABLENAME2.SPONO AND TABLENAME1.SLLNNO = TABLENAME2.SPLNNO)  
  GROUP BY TABLENAME1.SLONO, TABLENAME1.SLLNNO 
  </code></pre>