I have the following query to put some data from rows in columns:
string qry3 = "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 ";
But when I try to run it in the following code:
try { conAS400.Open(); iDB2Command command = conAS400.CreateCommand(); command.CommandText = qry3; command.CommandTimeout = 0; iDB2DataReader reader = command.ExecuteReader(); dtAS400 = new DataTable(); dtAS400.Load(reader); //<-- The exception occurs here. I can't even catch it conAS400.Close(); retrieved = true; } catch (Exception ex) { MessageBox.Show("Could not retrive the information. Exception: " + ex.Message); retrieved = false; } finally { conAS400.Close(); }
I get this exception:
“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..'”
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):
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
And it works. What can I do?
Drivers:
Client Access ODBC Driver (32-bit) 12.00.00.00
iSeries Access ODBC Driver 12.00.00.00
EDIT: Here’s the sql query without being a string.
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
Advertisement
Answer
OK, so I finally found out what was the problem. For some reason, I’m not able to CAST
to float inside the MAX
. If I remove both CAST
from the query, the exception is gone and everything works. I’m not sure why this happens, and if anyone could explain that, it would be very helpful as I need to cast those values to float. Also, if I SELECT
more columns, it is necessary to add said columns into the GROUP BY
statement. Anyways, here’s the modified code that works:
EDIT: Found out the way to cast them. It turns out I was getting some nulls, some blank fields, and I was trying to assign a number to a string. This is the code now:
SELECT TABLENAME1.SLONO AS ORDER, TABLENAME1.SLLNNO AS LINE, TABLENAME1.PROMISEDT, CAST(MAX(CASE WHEN RTRIM(TABLENAME2.CZREFD) IS NULL OR RTRIM(TABLENAME2.CZREFD) = '' THEN '0.000' WHEN TABLENAME2.CZVRNM = 'SLEEVEDEPTH' OR TABLENAME2.CZVRNM = 'LENGTH' THEN RTRIM(TABLENAME2.CZREFD) END) AS FLOAT(53)) AS SLEEVE, MAX(CASE WHEN TABLENAME2.CZVRNM in ('INSTALLATION') THEN RTRIM(TABLENAME2.CZREFD) 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, TABLENAME1.PROMISEDT --remember to add every column from the SELECT here