Code 4004

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;

4004

 

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"

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;

decent error

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;

  1. The debugger does not appear to catch all exceptions
  2. 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;

        private void machineDomainDataSource_SubmittedChanges(object sender, SubmittedChangesEventArgs e)
        {
            if (e.HasError)
            {
                System.Diagnostics.Debug.WriteLine(e.Error.ToString());
                e.MarkErrorAsHandled();
            }
        }

 

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…

26 comments to Code 4004 “Unhandled error in silverlight application”

  • Shadowdog

    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

  • Bebandit

    Thanks! That was exactly the problem and the fix. It's VS 2010, can't they figure this out yet?

  • Mickie

    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

  • kus3000

    Don't understand. I copy in my xaml your code, and when I execut it, it never enter machineDomainDataSource_SubmittedChanges.
    What is the problem ?

  • Ehsan Khodarahmi

    Thanks, that really helped me !

  • Gabor

    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

  • 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

  • hardika

    Hiii
    i m getting same error 4004. but i m not using datasource. i have used WCF
    PLEASE HELP ME

  • Vince

    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.

  • il

    I used WCF and getting same error 4004
     

  • Ram

    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.

  • Tema Shore

    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

  • Reza

    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

  • Rwsar

    I am getting the same error when click onn  browser back button .please help me to fix that bug.Unhandled exception 4004.

  • prachi

    I m also getting the the same exception while cliking on button..so plz…help me for solving this problem……..

  • Gautham

    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
     
     

  • siclesia

    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.

  • Steve Osborne

    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
    }

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <font color="" face="" size=""> <span style="">