views:

97

answers:

2

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.AttachTo("Products", p);
                //ctx.BeginSaveChanges();
                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.

A: 

please on your service turn on the UserVerboseErrors

config.UseVerboseErrors = true;

And it would be helpful to override the method handleexception

protected override void HandleException(HandleExceptionArgs args)

After you have done that let us know what the error is.

dmportella
But if i were to bet ont he root of the problem it would be that youa re using a object (p) that was fetched from data service using a different context then the one you used to save with it.
dmportella
After add config.UseVerboseErrors = true; and make sure both data binding to listbox and save button use same context(Create context at class level, not method level), I still get same error.
KentZhou
but what error are you getting? Look at the inner exceptions for the service, ie put a breakpoint on the handleexception overload and see what is the actual error.
dmportella
A: 

Is this for a Silverlight client or WinForms? Why did you switch from using BeginSaveChanges() to SaveChanges()?

related questions