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 !
Thanks for the explanation, but I'm not using the domain datasource, but the context from within code, and get the same error.
How can I subscribe to SubmittedChenges eventhandler?
Thanks Gabor
I didn't found the Detach Alln option under debug menu. Could anybody help me where to find them?
Thanks
Gabor
Hi Gabor
sorry for the delay in responding. You will only see the "Detach All" option when the debugger is actually running (in other words, you pressed F5). If your project is not running at all then that's an entirely different problem to the one I present here.
So, in summary, build your project and run it in debug mode. If you get the above message (about an unhandled exception) and the error 4004, then this article may be of interest to you. If you can't get your silverlight app to start, then this is probably a red herring…
Maybe upside down in my responses. I'm not sure what you're asking re datasources. I suspect the issue is the same, though; get the code running and catch all errors. See http://msdn.microsoft.com/en-us/library/cc189070(VS.95).aspx for more info
Hiii
i m getting same error 4004. but i m not using datasource. i have used WCF
PLEASE HELP ME
Thank you very much. I was getting the same error but not on the submit changes event. I was getting the error on the LoadedData event. I put your code in the LoadedData event and that fixed my problem. This seems like a big error and not documented well. This may be the same for some of the others developers.
I used WCF and getting same error 4004
I am also getting same problem either using navigation or bussiness application while using WCF RIA service
I did'nt use any data source
where to write code
if (e.HasError)
{
System.Diagnostics.Debug.WriteLine(e.Error.ToString());
e.MarkErrorAsHandled();
}
On the data source, add an event handler for SubmittedChanges. Another poster suggested the LoadedData event.
The key, though, is to detach the debugger so that you can see where the exception happened, and then put the code in that method.
I am getting the same exception and cannot figure out twhere the exception is coming from. I tried detach but it does not seem to do the trick…
Can someone help me?
Thanks
Hello ,I am getting the same exception.
I have combobox and datagrid that both have domaindatasource.for which one I need add this code?
Regards
I am getting the same error when click onn browser back button .please help me to fix that bug.Unhandled exception 4004.
I m also getting the the same exception while cliking on button..so plz…help me for solving this problem……..
Hello,
I was struggling on some error for quite along time. I am able to handle that after reading this blog.
Thankyou very much.
Regards,
Gautham
You can set-up a break point in App.xaml.cs @ private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
this will show the exception detail.
Thank you Siclesia. Your suggestion worked for me:
"You can set-up a break point in App.xaml.cs @ private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
this will show the exception detail."
Thanks for this little tidbit: I started hitting this error, though my circumstances were different. I'm using SIlverlight 5 and RIA services, but I'm doing most of the work in a derived class, and wiring up a Context in the code behind.
I have client side validation using class attributes, as well as a custom validator that can only run on the server, so to fix my problem here's what I had to do:
context.SubmitChanges().Completed += (so,ea) =>
{
if (((SubmitOperation)so).HasError) ((SubmitOperation)so).MarkErrorAsHandled();
// Rest of the work I do on completion
}
Thanks for that, Bryan