Search code examples
c#linqdatasetstrongly-typed-dataset

Datatable.GetChanges() always returns null


What I'm trying to do is edit the matching row and change a value for a column but it keeps returning null. I believe this is because of the way that I save the query to an object because if I access the query directly then it keeps processing the query everytime. What is the best way to handle this?

using (SymbolsTableAdapter symbolAdapter = new SymbolsTableAdapter())
using (Dataset.SymbolsDataTable symbolTable = new Dataset.SymbolsDataTable())
{
    symbolAdapter.Fill(symbolTable);

    foreach (var error in errors)
    {
        var query = from c in symbolTable
                    where c.Symbol == error.Key && c.Market == error.Value
                    select c;

        Dataset.SymbolsRow row = query.AsParallel().FirstOrDefault();

        if (row != null)
        {
            row.isUnderReview = true;
        }
    }

    // now save
    if (symbolTable.GetChanges() != null)
    {
        symbolTable.AcceptChanges();
    }
}

Solution

  • Ok I'm not sure why AcceptChanges wasn't actually doing anything but I changed my code a bit to the below and it works just fine for anyone's future reference

    Dataset.SymbolsDataTable tempSymbolsTable = new Dataset.SymbolsDataTable();
    tempSymbolsTable = (Dataset.SymbolsDataTable)symbolTable.GetChanges();
    
                    if (tempSymbolsTable != null)
                    {
                        symbolAdapter.Update(tempSymbolsTable);
                        tempSymbolsTable.Dispose();
                    }