MTH
MTH

Reputation: 37

ODBCDataReader sometimes returns false even when rows exist

My OdbcDataReader.HasRows returns FALSE only on the first read, but returns true for all other reads.

First I setup 2 ODBC readers to the same database connection. If I get the information I need from the first read, then I go to the 2nd reader and grab more information.

OdbcConnection MACMIRROR = new OdbcConnection("DSN=AS400 - Mac-Mirror");  
MACMIRROR.Open();  // This line opens MacMirror database

OdbcCommand MMCmd_ConfigCode_SerialNum_PartNum = MACMIRROR.CreateCommand();  
// Get Configuration Code, Serial number and Part number command.

OdbcCommand MMCmd_TextualInfo = MACMIRROR.CreateCommand();  
// Get Textural information out of MACPAC for specified serial number.

MMCmd_ConfigCode_SerialNum_PartNum.CommandText = "SELECT GSMCFG, GSMSER, GSMPRT FROM GST001PF
 WHERE GSMSER='" + Dispenser_SN.Golden_Dispenser_Serial_Number + "'";


MMCmd_TextualInfo.CommandText = "SELECT CXCFGC, CXEXCC FROM EC140M2 WHERE CXCFGC='" + 
 This_Dispenser.CompleteDispConfigCode + "'";

OdbcDataReader DBReader = MMCmd_ConfigCode_SerialNum_PartNum.ExecuteReader();
OdbcDataReader DBReaderTextInfo =  MMCmd_TextualInfo.ExecuteReader();

// Use serial number to get config code.
  if (DBReader.Read())  
      {
          This_Dispenser.CompleteDispConfigCode = (DBReader["GSMCFG"].ToString());  
          This_Dispenser.SetAllParametersFromConfigCode();
               //Got good config code, now use config code to get textual information.

           if (DBReaderTextInfo.Read())  
              // Does not read on the first attempt only 
              //   (had rows is false here, but true on 2nd time through code)
                {
                   This_Dispenser.TexturalInformation = (DBReaderTextInfo["CXEXCC"].ToString());
                   DBReaderTextInfo.Close();
                }
      }

 DBReader.Close();
 MACMIRROR.Close();  // Close the MacMirror connection    

Upvotes: 0

Views: 299

Answers (1)

MTH
MTH

Reputation: 37

I finally got this fixed. After creating and using the first ODBC reader, I had to close and dispose it. Then create a new ODBC reader, grab the data, then close and dispose (or NULL) that reader.

Upvotes: 1

Related Questions