1

In our project, we have multiple System.Data.Linq.DataContext objects for the different Windows forms.

When databound controls on the form are changed (like the Name), we enable or disable the Save Button by checking the GetChangeSet method.

private void ContentChanged(object sender, EventArgs e)
{
    var changes = GetChangeSet();
    int numChanges = changes.Deletes.Count + changes.Inserts.Count + changes.Updates.Count;
    btnSubmit.Enabled = (numChanges > 0);
}

When the Save Button is clicked, we call SubmitChanges on the Linq DataContext followed by a call to a ClearCache extension on the DataContext:

bool IFormPanelAccessor.CommitChanges<T>(T record)
{
    _dc.SubmitChanges();
    _dc.ClearCache();
}

ClearCache is defined in a static extensions class as follows:

public static void ClearCache(this DataContext dc)
{
    dc.GetType().InvokeMember("ClearCache", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, dc, null);
}

After saving, subsequent changes in the DataContext are no longer detected with GetChangeSet.

There is a trigger on the database that is called after an Insert or Update. Would this somehow break the DataContext's ability to detect changes?

How do I get GetChangeSet to detect changes on databound controls after a call to SubmitChanges?

6
  • Does it work if you remove the call to ClearCache()? Re: the trigger, depends what it does :) Commented Apr 5, 2018 at 15:35
  • Are you changing your connection string when you are switching DataContext? Are you going to different Server,Different Databases on same server, or just different tables in same database? Commented Apr 5, 2018 at 15:51
  • @ardila, removing ClearCache() does solve that particular issue, but then other parts of the form do not update to see the changes. Commented Apr 5, 2018 at 18:34
  • @jdweng, this project uses the same connection string throughout. The Data Context is loading a view (a join of multiple tables). Commented Apr 5, 2018 at 18:36
  • 1
    This sound similar to DGV updates when DataSource is a DataTable. The DGV doesn't automatically get updated when the datasource changes (in this case the datatable). The form repaint doesn't get called so the data doesn't get updated. The trick is to set the DGV datasource to null and then back to the DataTable which forces the DGVl to get repainted. Commented Apr 5, 2018 at 21:44

1 Answer 1

0

Similar to jdweng's comment in the original question, I found a way to reload the data.

private void CommitChanges()
{
    // mCustomer is a record from the CustomerRecords Linq DataTable
    var rowId = mCustomer.RowId;
    _dc.SubmitChanges();
    _dc.ClearCache();
    // After ClearCache, it seems I need to physically pull the data again:
    mCustomer = _dc.CustomerRecords.FirstOrDefault(c => c.RowId == rowId);
    // Use the new record to update the controls on the form
    LoadData(mCustomer);
}

My guess is that ClearCache was causing the current record to lose its connection to the data.

Seems simple in hindsight, but I just never realized that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.