locked
Can not update data using ADO.NET Service RRS feed

  • Question



  • I have a very simple exercise on ADO.NET Service: Put the list of Product in a list box, when selection changed in the list box, display UnitPrice and UnitInStock in two textboxes. Then change the data in textboxes and save the changes.

    Here is all code for at client side:

    namespace TestApp
    {
        public partial class Form1 : Form
        {
            NorthwindDataContext ctx = new NorthwindDataContext(new Uri("http://localhost:3540/Northwind.svc/"));
            public Form1()
            {
                InitializeComponent();
                var q = from p in ctx.Products
                        select p;
                listBox1.DataSource = q.ToList();
                listBox1.DisplayMember = "ProductName";
            }

            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                var p = listBox1.SelectedItem as Product;
                textBox1.Text = p.UnitPrice.ToString();
                textBox2.Text = p.UnitsInStock.ToString();
            }

            private void button1_Click(object sender, EventArgs e)
            {    
                var p = listBox1.SelectedItem as Product;
                p.UnitPrice = Decimal.Parse(textBox1.Text);
                p.UnitsInStock = short.Parse(textBox2.Text);
                try
                {
                    ctx.UpdateObject(p);
                    ctx.SaveChanges(SaveChangesOptions.None);
                }
                catch (Exception ex)
                {
                    label3.Text = ex.Message;
                }
            }
        }
    }

    The ADO.NET Service is fine and its permission set as:

     config.SetEntitySetAccessRule("*", EntitySetRights.All);

    When I hit the save button, I got error message as: ex.Message = "An error occurred while processing this request."

    Not sure why. please help.
    Wednesday, September 23, 2009 1:09 AM

Answers

All replies

  • Hi,
    The error occurs because you use the ToList() method and therefore your entities aren't being tracked by the context.
    UpdateObject works only with entity instances that are already being tracked by the context and therefore you get an exception.
    to solve the problem, before the call for UpdateObject you need to attach it using the AttachObject method.

    For example:
    ctx.AttachObject("Products", p);
    ctx.UpdateObject(p);
    ctx.SaveChanges(SaveChangesOptions.None);

    I hope it will help you.

    Gil Fink
    Wednesday, September 23, 2009 7:54 AM
  • Thank you. As I changed the code like:
       private void button1_Click(object sender, EventArgs e)
            {    
                var p = listBox1.SelectedItem as Product;
                p.UnitPrice = Decimal.Parse(textBox1.Text);
                p.UnitsInStock = short.Parse(textBox2.Text);
                try
                {
                    ctx.AttachObject ( "Products " , p) ;
                    ctx.UpdateObject(p);
                    ctx.SaveChanges(SaveChangesOptions.None);
                }
                catch (Exception ex)
                {
                    label3.Text = ex.Message;
                }
            }

    Then I get message as:
    ex.Message = "The context is already tracking the entity. "
    Wednesday, September 23, 2009 1:50 PM
  • Hi,
    My mistake. Remove the AttachObject method.
    Please put on the service the [ServiceBehavior(IncludeExceptionDetailInFaults=true)] in order to get the underling exception that you get when you save changes. Please publish the error here for further inspection.
    Gil Fink
    Wednesday, September 23, 2009 2:32 PM
  • Here is the information in detail after add the attribute for the service class as you suggested:

    -        InnerException    "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n<error xmlns=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\">\r\n  <code></code>\r\n  <message xml:lang=\"en-CA\">The data source must implement IUpdatable to support updates.</message>\r\n</error>\r\n"    System.Exception {System.Data.Services.Client.DataServiceClientException}
    Wednesday, September 23, 2009 3:11 PM
  • Hi,
    The error you get is due to the reason that the data source of the data service didn't implement the IUpdatable interface. Since Entity Framework implement this interface by deafult I guess you use LINQ to SQL or custom provider.
    In the following link you can find an IUpdatable implementation for LINQ to SQL: http://code.msdn.microsoft.com/IUpdateableLinqToSql

    If you use custom provider as your data source you can use my suggested solution in the following link:
    http://blogs.microsoft.co.il/blogs/gilf/archive/2008/08/31/how-to-perform-crud-operations-in-ado-net-data-services-with-custom-provider.aspx

    I hope it will help you.
    Gil Fink
    • Marked as answer by KentZhou Wednesday, September 23, 2009 5:25 PM
    Wednesday, September 23, 2009 3:26 PM
  • Thank you very much. Yes, I use Linq to SQL for DAL. After changing it to EF, I can update data. 
    Wednesday, September 23, 2009 5:24 PM