views:

24

answers:

1

Hi,

I have a Silverlight client with a grid getting data from WCF Data Service. Works fine.

However if I want to update some changed grid row, the service data context UpdateObject is not working:

    DataServiceContext.UpdateObject(MyGrid.SelectedItem);
    foreach (Object item in DataServiceContext.Entities)
    {
        //
    }
    DataServiceContext.BeginSaveChanges(SaveChangesOptions.Batch, OnChangesSaved, DataServiceContext);

I just have created a loop to inspect the values for the entities items and the value is not updated at all. BeginSaveChanges works fine, but it just uses not updated values.

Any ideas how to fix that?

thanks

A: 

Right a fully flushed out SaveChanges that will show the error message if EndSaveChanges() fails, like the code sample below. Obviously you can't use the console to write out your message in silverlight, but you get the idea.

For instance, when I wrote the following sample, I found that I was getting a forbidden error, because my entity set had EntitySetRights.AllRead, not EntitySetRights.All

 class Program
    {
        private static AdventureWorksEntities svc;

        static void Main(string[] args)
        {
            svc =
                new AdventureWorksEntities(
                    new Uri("http://localhost:5068/AWDataService.svc", 
                        UriKind.Absolute));
            var productQuery = from p in svc.Products
                    where p.ProductID == 740
                    select p;
            var product = productQuery.First();
            ShowProduct(product);
            product.Color = product.Color == "Silver" ? "Gray" : "Silver";
            svc.UpdateObject(product);
            svc.BeginSaveChanges(SaveChangesOptions.Batch, OnSave, svc);
            ShowProduct(product);
            Console.ReadKey();
        }

        private static void ShowProduct(Product product)
        {
            Console.WriteLine("Id: {0} Name: {1} Color: {2}", 
                product.ProductID, product.Name, product.Color);
        }

        private static void OnSave(IAsyncResult ar)
        {
            svc = ar.AsyncState as AdventureWorksEntities;
            try
            {
                WriteResponse(svc.EndSaveChanges(ar));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private static void WriteResponse(DataServiceResponse response)
        {
            if(response.IsBatchResponse)
            {
                Console.WriteLine("Batch Response Code: {0}", response.BatchStatusCode);
            }
            foreach (ChangeOperationResponse change in response)
            {
                Console.WriteLine("Change code: {0}", change.StatusCode);
                if(change.Error != null)
                {
                    Console.WriteLine("\tError: {0}", change.Error.Message);
                }
            }
        }
    }
Tim Hoolihan