Solved

CDaoRecordset: "Item not found in this collection"

Posted on 2001-08-07
8
371 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
[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
  • 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
Independent Software Vendors: 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!

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
substring method in java 1 136
zeroFront challenge 7 124
mapAB Challlenge 35 174
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 1,325
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.

726 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