Edit; 26th May 2010; This is my most favorite post! Check out related Silverlight posts here…
Just getting started in Silverlight 4 WCF RIA Services (which is astonishing, by the way – check out Brad Abram's walkthrough here) but I was getting stuck at the validation part.
When I exercise the validation (you know, red bits in the browser because you're failing metadata requirements) and SubmitChanges, it was throwing up a horrible error;
An unhandled exception ('Unhandled Error in Silverlight Application
Code: 4004
Category: ManagedRunTimError
Message: System.ServiceModel.DomainServices.Client.DomainException: And error occurred while submitting changes…
The only funny bit about this was the typo; "And error occurred"… The dialog is shown here;

OK – I got an exception, let's debug… So, I click on "OK" and get the message "Unable to attach to the crashing process. A debugger is already attached"

Which it is, of course. The one question I have through this is why the heck does the running debugger not catch this?
OK, so let's re-run and this time, detach the debugger by going to the Debug menu and select "Detach all". Then I made it crash by entering an invalid value and trying to save again and, this time, I get this;
.png)
At least now I know what the issue is. Notice that, no surprise, the error text still begins "And error occurred…"
So here's what I learned;
- The debugger does not appear to catch all exceptions
- I need to handle the validation error situation properly in the _SubmittedChanges event
This all comes down to two issues. One, you need to explicitly check whether the _SubmittedChanges() succeeded and 2) Visual Studio explodes if you don't do this.
OK, let's fix it
The trick is to let poor old SilverLight know that you're happy with the validation exceptions being handled in the framework.
Right-click the datasource, go to the events pane in properties, and add an event handler for "SubmittedChanges".
If you're UI-averse, then add the event handler manually in the XAML;
<riaControls:DomainDataSource AutoLoad="True" LoadedData="machineDomainDataSource_LoadedData" Name="machineDomainDataSource" QueryName="GetMachinesQuery" Width="0" SubmittedChanges="machineDomainDataSource_SubmittedChanges">
The key is the SubmittedChanges tag that calls machineDomainDataSource_SubmittedChanges when we submit changes.
Looking at the machineDomainDataSource_SubmittedChanges method, all we need is;
The [ System.Diagnostics.Debug.WriteLine(e.Error.ToString()); ] is completely optional. The absolute killer point is recognising that we have an error and then telling Silverlight that we handled it. Without this code, an exception is thrown.
With the code in place then validation happens as you would expect, within the browser and the application continues as if nothing happened.
I've no idea why the standard VS2010 Debugger doesn't catch that exception but at least we know what it is, now.
Edit; 26th May 2010; This is my most favorite post! Check out related Silverlight posts here…

Thanks a ton for your explanation… I was totally banging my head… Still haven't fixed the problem (Exception says I have no Insert on my DataSource?)
Thanks again
Pleasure!
Could it be telling the truth? Is there an Insert method on the underlying service?
Thanks! That was exactly the problem and the fix. It's VS 2010, can't they figure this out yet?
I agree - bizarre, isn't it?
Just wanted to write in and say thanks very much for posting this. 'Detach All' has been very helpful in troubleshooting some issues that I ran into with a DataGrid/DataForm implementation. The exact issue above being one of them. Thanks!
Thanks for taking the trouble to respond, Mickie. I come across these weird scenarios and think "I wonder whether that affects anyone else" and slap 'em up hoping they will help. Comments like yours confirm that this is a useful exercise.
Regards
Scott
Don't understand. I copy in my xaml your code, and when I execut it, it never enter machineDomainDataSource_SubmittedChanges.
What is the problem ?
Thanks, that really helped me !