views:

78

answers:

2

Hello! I have a project setup - it is a Silverlight Client Application hosted in an ASP.Net site. It has an ADO.Net Entity Framework to communicate with an SQL Server Database and an ADO.Net Data Service for communication. I am having some trouble getting my Asynchronous CRUD Silverlight Insert to work on my database. The first method fires fine and passes in the URI. But when the"OnClientJobQueryComplete" method fires, it fails about 5 lines down and i cannot understand why. The exception says "An error occured while processing this request."

private void addStuff_Click(object sender, RoutedEventArgs e)
    {
        // Define a URI that returns the product with the specified ID.
        Uri jobrefUri = new Uri(svcContext.BaseUri.AbsoluteUri
            + "/ClientJob(" + this.jobref.Text + ")");

        // Begin a query operation retrieve the Product object 
        // that is required to add a link to the new Order_Detail.
        svcContext.BeginExecute<ClientJob>(jobrefUri,
            OnClientJobQueryCompleted,null);
    }

    private void OnClientJobQueryCompleted(IAsyncResult result)
    {
        // Use the Dispatcher to ensure that the 
        // asynchronous call returns in the correct thread.
        Dispatcher.BeginInvoke(() =>
        {
            // Get the Product returned by the completed query.
            IEnumerable<ClientJob> queryResult =
                svcContext.EndExecute<ClientJob>(result);//**TRIES THIS BUT FAILS HERE

            ClientJob returnedClientJob = queryResult.First();

            // Get the currently selected order. (Create new Guid since not Northwind)
            Guid g = Guid.NewGuid();

            // Create a new Order_Details object with the supplied FK values.
            Job newItem = Job.CreateJob(g);
            //Job newItem = Job.CreateJob(g, returnedClientJob.JobRef);

            jobsBindingCollection.Add(newItem);

            // Add the new item to the context.
            svcContext.AddToJob(newItem);

            //// Add the relationship between the order and the new item.
            //currentOrder.Order_Details.Add(newItem);
            //svcContext.AddLink(currentOrder, "Order_Details", newItem);

            //// Set the reference to the order and product from the item.
            //newItem.Orders = currentOrder;
            //svcContext.SetLink(newItem, "Orders", currentOrder);

            // Add the relationship between the product and the new item.
            returnedClientJob.Job.Add(newItem);
            svcContext.AddLink(returnedClientJob, "Job", newItem);

            // Set the reference to the product from the item.
            newItem.ClientJob = returnedClientJob;
            svcContext.SetLink(newItem, "ClientJob", returnedClientJob);
        }
        );
    }

This code is lifted and modified from this Microsoft tutorial which uses the Northwind database. All of the other code samples from the tutorial work fine since my database is of a similar structure to Northwind. i have been able to implement RUD so far but not CRUD.

Could anyone shed any light on the subject?

Help greatly appreciated!

+1  A: 

You may need to look at your entities access rights. On one of the steps of the tutorial you cite this is configured but in some cases was set to "AllRead." You will need to make sure that your entitites allow write access.

David in Dakota
Have tried that one already im afraid, doesn't seem to make a difference :-(
Goober
A: 

Right now there are a lot of layers that make it hard to tell exactly where the problem is occurring.

When you call the query from a windows app directly to the EF model does it work? If not, something is probably wrong in your model.

Can you access the ADO.NET data service from a browser?

Do you have a clientaccesspolicy.xml file on the server that is hosting your service?

Are you running the silverlight app from a http:// location and not a file:// location?

Jacob Adams
I had some success - having changed this line: Uri jobrefUri = new Uri(svcContext.BaseUri.AbsoluteUri + "/ClientJob(" + this.jobref.Text + ")");to Uri jobrefUri = new Uri(svcContext.BaseUri.AbsoluteUri + "/ClientJob('" + this.jobref.Text + "')"); (As you can see there has been the inclusion of single quotes to encapsulate the value to search by with. However although this works i'm not entirely sure this is the correct implementation....
Goober