Solved

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

Posted on 2007-03-21
7
1,527 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
[X]
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
7 Comments
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 18769247

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

 
0
 
LVL 4

Author Comment

by:carlmahon
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);
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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
LVL 39

Expert Comment

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

Author Comment

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

Accepted Solution

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

Author Comment

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

Regards
Carl
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

688 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