Databinding issue with CslaDataProvider after exception

Databinding issue with CslaDataProvider after exception

Old forum URL: forums.lhotka.net/forums/t/3812.aspx


wleary posted on Tuesday, October 30, 2007

Greetings,

We are trying to use the CslaDataProvider in a WPF application. Everything works fine when no exceptions are encountered. However, when an exception is thrown during Save(), the data bindings are becoming lost. We have a TextBlock and TextBox that are bound to the object, and they work fine when we insert or update with no errors, but after the Save(), when an exception is encountered, they both become blank. We have spent a few hours now on this, and pulling hair out. We are listening to the DataChanged event, as the CSLA 3.0 book directs us. We see it getting called, and we can show the proper exception, but then the object appears to be unhooked from the data screen. Any help will be greatly appreciated.

Thanks,

William

wleary replied on Tuesday, October 30, 2007

We believe we have found the problem. The CslaDataProvider calls OnQueryFinished at the end of Save passing both the object and the exception. However, anytime there is an exception, the UpdateWithNewResult method of the DataSourceProvider base class sets the object's data to null, as the code below shows: (The following is from Reflector.)

private void UpdateWithNewResult(Exception error, object newData, DispatcherOperationCallback completionWork, object callbackArgs)
{
    bool flag = this._error != error;
    this._error = error;
    if (error != null)
    {
        newData = null;
        this._initialLoadCalled = false;
    }
    this._data = newData;
    if (completionWork != null)
    {
        completionWork(callbackArgs);
    }
    if (this.DataChanged != null)
    {
        this.DataChanged(this, EventArgs.Empty);
    }
    if (flag)
    {
        this.OnPropertyChanged(new PropertyChangedEventArgs("Error"));
    }
}

So, to fix this, we modified the CslaDataProvider so that the exception is passed in the inital call that is designed to clear the object's data, like so:

base.OnQueryFinished(null, exceptionResult, null, null)

Then, we removed the exception that is passed in the last line, like so:

base.OnQueryFinished(result, null, null, null)

Thanks,

William

RockfordLhotka replied on Tuesday, October 30, 2007

Interesting, thanks for posting this!

Copyright (c) Marimer LLC