Solved

CDaoRecordset: "Item not found in this collection"

Posted on 2001-08-07
8
347 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Importing  SSL Certificate into Firefox using GPO 28 202
Get filename and folder into excel 7 68
changeXy challenge 13 67
firstChar challenge 13 106
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

929 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

16 Experts available now in Live!

Get 1:1 Help Now