Solved

CDaoRecordset: "Item not found in this collection"

Posted on 2001-08-07
8
334 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
Comment Utility
<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
Comment Utility
before calling SetFieldValue() you have to call Edit().

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

Expert Comment

by:missionImpossible
Comment Utility
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
 

Author Comment

by:Claude050897
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Claude050897
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

743 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

10 Experts available now in Live!

Get 1:1 Help Now