Visual C++ MFC CRecordset MoveNext() error in MYSQL Database

Posted on 2007-03-21
Last Modified: 2013-11-20
I'm using Visual C++ ver 6.0 and have a MFC CRecordset class that connects to a readonly MYSQL database using the 3.51 MYSQL ODBC driver. The problem occurs during the MFC MoveNext() fuction when retrieiving multiple rows. The MoveNext() throws an eception with an empty error string on some records (not all) but if the error is trapped and ignored all of the records are properly retrieved. I know that VC 6.0 is dated and possibly this needs to be ported to 2005 but does anyone have an idea of what may be happening?
Question by:carlmahon
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 11

Expert Comment

ID: 18769247

Check out this link: The problem is in ODBC dBASE Driver though.

A good read on CRecordSet:

Can you post your code snippet please?

Best Regards,
LVL 39

Expert Comment

ID: 18769609
>>>> The MoveNext() throws an eception
Is it really an exception? Or a debug assertion?  In the latter case you could break into the debugger and check the MFC sources around the assertion statement. There are mostly comments that tell what was wrong. In case of an exception check your output window. Most ODBC drivers are passing error messages to the debugger.

Did you check IsBOF() and IsEOF() member fucntions? Maybe the recordset already was scrolled past the end and throws an exception because of that. Then you have to call a MoveFirst before calling MoveNext.

Regards, Alex


Author Comment

ID: 18772352
Thanks, I'm checking out the the meantime here is the code snippet. I ran a debug and it apperas that this is a field truncation issue (see debug code snippet at bottom)

CTimeIPSevent tEvents(&tDatabase);
     iTime = atol((const char *) tEvents.m_timeIn);
     oTime = atol((const char *) tEvents.m_timeOut);
     CATCH(CDBException, e)
           // The error code is in e->m_nRetCode
           CString msg;
            msg.Format("Error: %s\n\n",e->m_strError);
            MessageBox(msg,"SQL Error!");

This is where the debug assertion is...*******************************************************************

void CRecordset::CheckRowsetError(RETCODE nRetCode)
      CDBException e(nRetCode);
      // Build the error string but don't send nuisance output to TRACE window
      e.BuildErrorString(m_pDatabase, m_hstmt, FALSE);
            if (e.m_strStateNativeOrigin.Find(_afxDataTruncated) >= 0)
            // Ignore data truncated warning if binding long binary columns
            // (may mask non-long binary truncation warnings or other warnings)
            if (!((m_pDatabase->m_dwUpdateOptions & AFX_SQL_SETPOSUPDATES) &&
                                                                 --->TRACE0("Error: field data truncated during data fetch.\n");
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 39

Expert Comment

ID: 18772736
Check your DoFieldExchange function. In case of a text field the wizard created a RFX_Text macro call which defaults to 255 char max. If you have VARCHAR2 fields greater than that they get truncated. You can expand the RFX_Text passing an additional (max) size argument (3rd argument), e. g. 1024 or more.

Regards, Alex

Author Comment

ID: 18773420
Thanks Alex - I have checked the 255 limit and there are NO VARCHAR2 fields in the DB. Just as a test there were two fields that were varchar(255) and I added the third parm to the RFX_text call but no change.

Unfornutaley I only have read acces to this database and usually (in my own DB's) I will play aroud with the fields to see what happens in the RFX. I also cannot get into the AFX field exchange call in the debugger either to see what exact column/data is causing the random issue.

Since this is a truncation error and the data fileds that I need are not affected I did an override of CRecordset::CheckRowsetError() and am now simply ignoring any errors related to a truncation.

This is working just fine but as a programmer it always nice to know what is the root of the issue since it could affect a field that someone really needs. I would like to keep this open today to see if there is any other input and a solid answer, then I will awards points accordingly.
LVL 39

Accepted Solution

itsmeandnobodyelse earned 500 total points
ID: 18778320
Hmmm, truncation errors also happen if you supply a float where a double is required, or a short where you would need an int, or a byte (e. g. bool type) where you need an int as well. Also fixed char arrays could give a problem e. g. with timestamps, but DoFieldExchange uses a CString so I rather think that isn't a problem. I assume you are not using BLOBS, CLOBS, or other huge data fields. You could find out what statement is responsible by either debugging the DoFieldExchange (step over each RFX_ statement and when the exception was thrown you know where it goes wrong) or by commenting one RFX_ until the error was gone.  

If it happened somewhere in a greater result set you may spot the statement in the MFC sources where the exception was catched and put your brekpoint there. Then step up the call stack until you the RFX_ statement which cause the error. You also should see what the truncation issue is when evaluating the variables there.

Regards, Alex

Author Comment

ID: 18780309
Alex, thanks again - You were correct it was an RFX issue. There was a field designated simply as 'text' in the MySQL table, I was looking for varchar(??) or varchar2(??). This field in certain records was exceeding the 255 char RFX default limit. I changed the third parm to 1024 and behold no more errors. I learned a lot from this and your help was invaluable, nice job and all points to you.


Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question