?
Solved

CDaoRecordset: "Item not found in this collection"

Posted on 2001-08-07
8
Medium Priority
?
397 Views
Last Modified: 2013-11-20
I'm going mad! Why do I get this message?

1) I open the recordset with:
m_rsTitle.Open (dbOpenDynaset, "SELECT TitleID, Title FROM Title");

2) Everytime I want to read or write to the set I do:
m_rsTitle.SetFieldValue ("Title", szBuffer);

With each read or write I get "Item not found in this collection" I've tried using SetFieldValue (1, szBuffer) but it gives the same error. The weird thing is that just yesterday it worked for the first record that I add,
but then after that it only gave the error. And it is not just with either GetFieldValue() or SetFieldValue(), but with both.

Any suggestions?

For each dlg that uses the database I create
CDaoDatabase m_dbDatabase;
and open it in OnInitDlg()

I also declare for each record set
CDaoRecordset m_rsRecordSet;
and also open it in OnInitDlg()

I then close these in the destructor method of the class.

May this is the problem? Is it better to open the database and close it in the destructor but open and close the record set in each method?



0
Comment
Question by:Claude050897
  • 5
  • 3
8 Comments
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6359959
<May this is the problem? Is it better to open the database and close it in the destructor but open and
close the record set in each method>
no this is not better.

Are there records in your Set?:Try:
 rsRecordSet->MoveLast();
 lon lRecCount = rsRecordSet->GetRecordCount();

if so, is your rs updateable?:
 rsRecordSet->CanUpdate()



0
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6359979
before calling SetFieldValue() you have to call Edit().

Does GetFieldValue() really doesn't work?
0
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6360023
maybe the problem is with multiple database-objects: Try using only one database-object once. if this works, make your database-object static (singleton) and use it from every dlg.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Claude050897
ID: 6362828
1) I test for records in the set with
if (m_rsTitle.IsBOF() && m_rsTitle.IsEOF())
 return;

2) I test for CanUpdata()

3) I do use Edit / AddNew before calling SetFieldValue()
In one dlg I just wanted to read all values into a ListBox, but GetFieldValue() seems to give me this problem, but with other dlgs it works 100%

4) Mmm, this might be it. I have two db's that I work from and several recordsets from each. The db's and recordsets get opened with each dlg.

How do I make the database-object static?
0
 

Author Comment

by:Claude050897
ID: 6362973
Could it be that I declare the database and record set wrongly?

In the dlg class I have:

CDaoDatabase   m_dbTitle;
CDaoRecordset  m_rsTitle;

Note that I cannot use m_rsTitle(&m_dbTitle) here as the help file suggests.

Then in the InitDlg() I do

m_dbTitle.Open (...);
m_rsTitle.m_pDatabase = &m_dbTitle;

If this is the problem, then it means that I have to create a CDaoRecordset variable for each
method in the class and Open and Close the recordset for each method?
0
 
LVL 2

Accepted Solution

by:
missionImpossible earned 800 total points
ID: 6362980
Before you try this: please first be sure that this is the problem: maybe comment out all other db-accesses (and db-open)-calls, except the ones in the dlg that doesn't work. If the problem is solved with this, you can try the following, if not, please let us now (maybe you can post the code of the dlg.

if static or not: you can declare your database-object or a pointer of it in a class that can be accessed from every dlg, f.ex: doc-class, app-class, mainframe in your MFC-application, or another class, that capsulates the db-object as static object:

in your new class header:

class YourClass
{

protected:

  static CDaoDatabase m_dbDatabase:
}

in .cpp-file behind the includes (and outsight of all member-functions):

CDaoDatabase YourClass::m_dbDatabase; // will be initialized automatically at app-start.

this class you can instantiate f.ex. in your app, doc or mainfrm. let all dlg use this one and only database-object.


by the way: you get much more performance, if you open your db-connection once and close it by app-end instead of opening it in every dlg.

Good luck

0
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6362997
sorry the last comment is not based on your last comment. its based on that what you write before.

<Note that I cannot use m_rsTitle(&m_dbTitle) here as the help file suggests.>
what error occurs? at compile time?
this seems not the problem. Don't forget: each time opening a recordset is an access to the database - performance!

0
 

Author Comment

by:Claude050897
ID: 6372754
OK, I've done the following to get rid of the problem: I declared the database and recordsets as members in theApp instance and open and close them in theApp's InitInstance() and ExitInstance() methods. I hope I choose the right person here for the points...
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
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.
Suggested Courses

864 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