Solved

CRowset and MovePrev

Posted on 2002-05-08
6
312 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
[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
  • 2
6 Comments
 
LVL 3

Expert Comment

by:GGRUNDY
ID: 6998120
maybe you need

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

Expert Comment

by:DanRollins
ID: 7000066
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
ID: 7000138
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
Technology Partners: 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 49

Accepted Solution

by:
DanRollins earned 100 total points
ID: 7000348
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
ID: 7000468
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
ID: 7000628
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

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!

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.

752 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