CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266

    ADO AddNew gets no error but does not add

    I am using ADO and can read the recordset from beginning to end. When I get to the end I am trying to add records but they do not get added. I am using a SQL Select with adOpenDynamic for CursorType when opening the table. After opening the following are both true:
    Code:
    m_Recordset->Supports(ADODB::adUpdate)
    m_Recordset->Supports(ADODB::adAddNew)
    The following is a simplified version of what I am using to do the AddNew:
    Code:
    class CDataRecordset : public CADORecordBinding {
    BEGIN_ADO_BINDING(CDataRecordset)
      ADO_VARIABLE_LENGTH_ENTRY2(1, ADODB::adVarChar, m_DataKey, 
            sizeof(m_DataKey), m_DataKeyStatus, TRUE)
    END_ADO_BINDING()
    public:
    	CHAR m_DataKey[10];
    	ULONG m_DataKeyStatus;
    };
    
    	HRESULT hr=S_OK;
    strcpy(m_DataRecordset.m_DataKey, DataKey);
    m_DataRecordset.m_DataKeyStatus = 0;
    try {
    	TESTHR(hr = m_Recordset->AddNew());
    	TESTHR(hr = m_Recordset->Update());
    	}
    catch(_com_error err) {
    	// Show error
    	}
    catch(...) {
    	// Show error
    	}
    The EditMode is 0 before the AddNew, 2 after the AddNew and 0 again after the Update. There are no errors but thare are also no records added. Is there anything more I need to do to get the records added?
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  2. #2
    Join Date
    May 2000
    Location
    Toronto, ON, Canada
    Posts
    3,573
    Hi Sam,

    I don't know if you already took an eye at http://support.microsoft.com/default...;EN-US;Q190961
    Regards,

    Emanuel Vaduva

  3. #3
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Thank you.

    I did forget to search the current KB. I did search the MSDN I have installed in my system but it is an old copy. I also searched the current SDK documentation for AddNew. There are samples of AddNew in the SDK documentation. I did not see anything more that is needed in the samples I found. I will look at the sample in that KB article.

    I suspect that the problem might be related to the fact that I read the recordset first. If so then the samples are less likely to help. I posted this question in case someone experienced with ADO knows the answer. I have already spent a lot of time trying to find the answer but I am continuing to look.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  4. #4
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    That did it.

    The ReadMe.txt file for that sample in that KB article made clear something that the documentation does not make clear and that is easily overlooked in the sample. Now that I know the answer the documentation seems clear enough. The answer is that "you must use the AddNew and Update in the IADORecordBinding interface to make changes to the recordset". So I changed:
    Code:
    	TESTHR(hr = m_Recordset->AddNew());
    	TESTHR(hr = m_Recordset->Update());
    to:
    Code:
    TESTHR(m_RecordBinding->AddNew(&m_DataRecordset));
    and voilĂ* it works. So I am embarased that I did not look everywhere I should have for answers but I appreciate the help.
    Last edited by Sam Hobbs; March 18th, 2003 at 07:13 PM.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured