Solved

CRowset and MovePrev

Posted on 2002-05-08
6
290 Views
Last Modified: 2013-11-20
What is wrong with this code...


HRESULT hr = m_pSet->MoveLast();
while (hr == S_OK)
{
hr = m_pSet->MovePrev();
}


It is *supposed* to step backwards through the rowset once only, but what I find is that it just keeps going round and round infinitely and never exits the loop?!?

I am completely stumped... what am I missing here? (something obvious no doubt)

I tested the above code by creating a MFC app using the wizard to hook up an OLE DB connection to a Jet database. I am using VS.NET on Windows XP btw.
0
Comment
Question by:paulburns
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:GGRUNDY
Comment Utility
maybe you need

while (hr==S_OK && !m_pSet->IsBOF())
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
The docs ward agains using CRecordset::MovePrev() without chcking IsBOF().  So the best way to cycle backward would be analogus to
while(!crs.IsEOF() ) { ...crs.MoveNext()... }

while ( !m_pSet->IsBOF() ){
    m_pSet->MovePrev();
}

What I don't understand is why this is not giving you a compiler error:

     hr= m_pSet->MovePrev();

The CRecordset and CDaoRecordset member fns MovePrev() return void, and that attempt to assign the 'return value' of a void fn to your hr variable, is a clear error.  If you are using ADO _Recordsets, there is no MovePrev() fn (only MovePrevious).  

Can you explain?

-- Dan
0
 
LVL 2

Author Comment

by:paulburns
Comment Utility
Maybe I wasn't clear, I am using the OLE DB template classes... CRowset, not CRecordset. According to the docs MovePrev should return DB_S_ENDOFROWSET (i.e. something other than S_OK)  when it gets to the end or beginning of the rowset.


If I try this code to step *forward* through the rowset then it  works....


HRESULT hr = m_pSet->MoveFirst();
while (hr == S_OK)
{
hr = m_pSet->MoveNext();
}


It is only when stepping *backwards* through the rowset that I get the problem described.

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 49

Accepted Solution

by:
DanRollins earned 100 total points
Comment Utility
It really helps if you provide a contest.

In the file ATLDBCLI.H, we see that MovePrev is defined as :

HRESULT MovePrev()
{
     return MoveNext(-2, true); // delta, bForward
}

And the relvant part of MoveNext is:

HRESULT MoveNext(LONG lSkip, bool bForward)
      hr = m_spRowset->GetNextRows(NULL, lSkip, (bForward) ? 1 : -1, &ulRowsFetched, &phRow);

which is:
    GetNextRows( 0, -2, 1, &nCnt, &phRow )

So lSkip is -2 and cRows is 1 (requesting forward motion).  

The dox mention that providers implement skipping in various ways.  It seems that the Jet provider is a little wacky or perhaps the template is wrong (why not -1?  why move 'forward by -2 rather than move backward by 1?  

I'd have to guess that it rowset provider moves past the beginning and automatically cycles to the end and so sees no error.

Anyway, I'd try changing your code to this:

//----------------------- I did not test this code.
while (hr == S_OK)
{
     hr = m_pSet->MoveNext( 1, false );
    // or maybe
    // hr = m_pSet->MoveNext( 2, false );
}

That way the low-level GetNextRows() call will pass -1 as the third parameter, which tells the provider to skip backwards.

But the real solution is to check IsBOF() on each loop.

-- Dan
0
 
LVL 2

Author Comment

by:paulburns
Comment Utility
Hi Dan, you aren't Henry's brother by any chance? ;) ...ah poor joke I bet you get that all the time.

Anyway, after some experimenting I found MoveNext(0, false) worked. Now why the hell wasn't MovePrev
implemented this way?!

thanks.
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
OLE DB is designed to be very general.  I think that the template is implemented that way because some data sources have a lot of trouble moving backwards through a rowset (even though I can't think of why the template's handling would be any safer or more efficient).

-- Dan

BTW, you could have easily solved this yourself by single-stepping through the MovePrev fn; it's only about 10 lines of code.  VC++ debugger is excellent for solving mysteries.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 47
Windows Drag & Drop Location 2 85
windows 10 how make picture as desktop background 2 47
countEvens challenge 2 55
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli‚Ķ
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now