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

Posted on 2007-03-21
Medium Priority
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 responses...in 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");

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 2000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

762 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