Solved

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

Posted on 2007-03-21
7
1,473 Views
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?
0
Comment
Question by:carlmahon
  • 3
  • 3
7 Comments
 
LVL 11

Expert Comment

by:DeepuAbrahamK
Comment Utility

Check out this link: The problem is in ODBC dBASE Driver though.
http://support.microsoft.com/kb/120108

A good read on CRecordSet:
http://www.samspublishing.com/library/content.asp?b=Visual_C_PlusPlus&seqNum=192

Can you post your code snippet please?

Best Regards,
DeepuAbrahamK
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> 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

 
0
 
LVL 4

Author Comment

by:carlmahon
Comment Utility
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);
tEvents.Open(CRecordset::forwardOnly,"[eventLog]",CRecordset::executeDirect);
while(!tEvents.IsEOF()){
     iTime = atol((const char *) tEvents.m_timeIn);
     oTime = atol((const char *) tEvents.m_timeOut);
     SetColumnValue(tEvents.m_eventLogID,row,1,DT_LEFT,FALSE);
     SetColumnValue(tEvents.m_usersID,row,2,DT_LEFT,FALSE);
     SetColumnValue(tEvents.m_timeIn,row,4,DT_LEFT,FALSE);
     TRY
     {
         tEvents.MoveNext();
     }
     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!");
     }
    END_CATCH
}      

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

void CRecordset::CheckRowsetError(RETCODE nRetCode)
{
if (nRetCode == SQL_SUCCESS_WITH_INFO)
{
      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) &&
                  m_bLongBinaryColumns))
            {
                  NO_CPP_EXCEPTION(e.Empty());
                                                                 --->TRACE0("Error: field data truncated during data fetch.\n");
                  ThrowDBException(AFX_SQL_ERROR_DATA_TRUNCATED);
*************************************************************************************************************************
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
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
 
0
 
LVL 4

Author Comment

by:carlmahon
Comment Utility
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.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
Comment Utility
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
0
 
LVL 4

Author Comment

by:carlmahon
Comment Utility
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.

Regards
Carl
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now