Solved

Using AddNew with CDaoRecordset

Posted on 1998-11-26
1
542 Views
Last Modified: 2013-11-20
I am having trouble using CDaoRecordset::AddNew.   Basically I am trying to scan a table for a name.  If that name is not there, I want to add it.  Here is my code:


strQuery.Format("select ArtistID from Artists where ArtistName = '%s'", pszArtistName);
                  
CDaoRecordset* pRS = new CDaoRecordset(m_pDB);
CString cs("");

try
{
      pRS->Open(dbOpenDynaset, strQuery);

      int iRecords = pRS->GetRecordCount();

      if (!iRecords)
      {
            pRS->AddNew();
            pRS->SetFieldValue(2, pszArtistName);
            pRS->Update();
        }
}
catch (CDaoException* e)
{
      AfxMessageBox(e->m_pErrorInfo->m_strDescription, MB_ICONEXCLAMATION);
}

----

The problem is that every time I try to call SetFieldValue I get an exception that says "Item not found in this collection".   The new row (blank however) is added to the table, I just can't set my data for that row.

How can I accomplish this?
0
Comment
Question by:tdietz
1 Comment
 
LVL 3

Accepted Solution

by:
GGRUNDY earned 50 total points
ID: 1325353
The dynaset you created with your open statement only has one
column ( ArtistID), trying to update the second column therefor
gives the grief.

Try
strQuery.Format("select * from Artists where ArtistName = '%s'", pszArtistName);

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bobThere java chalenge 45 85
repeateFront java challenge 31 87
matchUp  challenge 6 48
modThree challenge 4 64
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…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

24 Experts available now in Live!

Get 1:1 Help Now