Solved

CDaoRecordset: "Item not found in this collection"

Posted on 2001-08-07
8
356 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
format the code in java 6 93
difference between String.subString() and String.subSequence() 6 193
sumHeights2  challenge 7 102
Detect file exist or not 3 155
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

806 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