简体   繁体   中英

dataset changes not getting updated in SQL Server database

I've created a data set via the Designer, and I'm able to retrieve data just fine. After I do some updates to other data, I need to change the status in the original row. I've scoured StackOverflow and MSDN, and I think I've tried everything suggested - no luck.

AFEDataSetTableAdapters.AFE_allTableAdapter taAFEAll = new AFEDataSetTableAdapters.AFE_allTableAdapter();
AFEDataSet.AFE_allDataTable dtAFEAll = new AFEDataSet.AFE_allDataTable();
taAFEAll.Fill(dtAFEAll, sAFENumber);
if (dtAFEAll.Count != 1)
{
    // error condition - should only ever be one row
}
DataRow drAFE = dtAFEAll.Rows[0];

// ...if everything goes well...

if (...successful...)
{
    drAFE.BeginEdit();
    drAFE["AFEStatus"] = "Published";
    drAFE.EndEdit();
    try
    {
        drAFE.AcceptChanges();
        int iTemp = taAFEAll.Update(dsAFE.AFE_all);
    }
    catch (Exception ex)
    {
        sMessage = ex.Message.ToString();
    }
}

I've tried it with and without the BeginEdit, EndEdit, AcceptChanges; no change. When the .Update method is invoked, I can see (in debug) that drAFE's ItemArray shows the changed value for AFEStatus. The Update returns a zero, however, and the database doesn't change.

I verified in the code behind the DataSetDesigner, and the AFE_all table has an Update statement defined.

Where did I go wrong? or, what one-more-step(s) am I missing?

You need to remove this line

 drAFE.AcceptChanges();

The AcceptChanges() method 'changes' the state of the DataRow to Unchanged, so when you call the Update there is no row to save.

From MSDN

When you call AcceptChanges on the DataSet, any DataRow objects still in edit-mode end their edits successfully. The RowState property of each DataRow also changes; Added and Modified rows become Unchanged, and Deleted rows are removed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM