0

I`m using the following Tutorial to create a Simple MFC application the problem is in inserting new rows in the table The application shows an error saying Recordset is Read-only. Is there any variable that needs to be set to change Recordset to write mode?

void CSampleDBView::OnBnClickedInsert()
{
    CStringW text;
    m_input.GetWindowTextW(text);
    m_pSet->m_student=text;
    m_pSet->AddNew();
    m_pSet->Update();
    AfxMessageBox(text);    
}

This is the code that Adds a new row when button is clicked. m_input is a EditControl From where the Text to be inserted is recieved.

AAB
  • 1,594
  • 2
  • 25
  • 40

1 Answers1

0

Check for how the CRecordset is being opened. It needs to have type 'CRecordset::dynaset' or CRecordset::dynamic' if you intend to update it. Your call to open might have 'snapshot' or some other type.

There is an additional problem with the posted code. The call to AddNew() needs to come before assignment lines:

m_pSet->AddNew();
m_pSet->m_student=text;

because the call to AddNew() initializes flags that are used to track which columns have changed data.

Mark Taylor
  • 1,843
  • 14
  • 17
  • Yes, snapshot means read-only. No Add or Edit allowed on a snapshot. – Mark Taylor Nov 04 '14 at 05:27
  • So While choosing data source if I choose Dynaset instead the problem goes away? Strange but when i read details for snapshot they never mentioned anything about read-only it was just said that a Requery() was required to show the new updates. – AAB Nov 04 '14 at 05:35
  • Thanks Let me give this way a try – AAB Nov 04 '14 at 05:36
  • No Luck with Dynaset either still I get error saying Update or Delete Failed – AAB Nov 04 '14 at 05:51
  • There are many other issues that can affect whether or not a CRecordset can be updated with various Open parameters. Dependencies include the database, the driver, and whether or not the table has a primary key. Here's a link to just one example: http://stackoverflow.com/questions/12714310/crecordsetsnapshot-doesnt-work-in-vs2012-anymore-whats-the-alternative – Mark Taylor Nov 04 '14 at 06:07