Answered by:
How to use WCFDataService to add or update data of SqlServer database in windows store app

Question
-
I have read these documents of WCFDataService for SilverLight:
How to: Bind Data Service Data to Controls (WCF Data Services/Silverlight)
-
How to: Project Data Service Query Results (WCF Data Services/Silverlight) .
-
I moved the codes to windows store app. Most of the codes worked well.
-
But when I wrote the following code in windows store app in order to add some data to database , I always got an error.
-
private void saveChangesButton_Click(object sender, RoutedEventArgs e)
{
try
{ -
// The data I want to add to database -
Orders orders1 = Orders.CreateOrders(1);
orders1.CustomerID = "ALFKI";
orders1.ShipCity = "beijing";
context.AddToOrders(orders1);
context.BeginSaveChanges(SaveChangesOptions.Batch,
OnChangesSaved, context);
}
catch (Exception ex)
{
tb1.Text = string.Format("The changes could not be saved to the data service.\n"
+ "The following error occurred: {0}", ex.Message);
}
}private void OnChangesSaved(IAsyncResult result)
{
bool errorOccured = false;
Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
context = result.AsyncState as NorthwindEntities;try
{
//I always got error after the following sentense
DataServiceResponse response = context.EndSaveChanges(result);foreach (ChangeOperationResponse changeResponse in response)
{
if (changeResponse.Error != null) errorOccured = true;
}
if (!errorOccured)
{
tb1.Text = "The changes have been saved to the data service.";
}
else
{
tb1.Text = ("An error occured. One or more changes could not be saved.");
}
}
catch (Exception ex)
{
// Display the error from the response.
tb1.Text = (string.Format("The following error occured: {0}", ex.GetBaseException().Message));
}
}
).AsTask().Wait();
} -
My file is herehttps://skydrive.live.com/#cid=D33A4DC661FB96BE&id=D33A4DC661FB96BE%21134 Please help me, thanks.
王林
Friday, September 20, 2013 1:59 PM -
Answers
-
The SOAP error message suggest that you do not have permission to insert items to the table/database.
If you setup the data service as described in How to: Create the Northwind Data Service ... then the "Orders" table probably does not have the EntitySetRights.WriteAppend permissions, which is required to add (append) items to the table.
-Eric.
- Marked as answer by wanglin005 Wednesday, September 25, 2013 11:20 AM
Tuesday, September 24, 2013 2:51 PMModerator
All replies
-
What error are you getting?
At what line of code?
-Eric.
Friday, September 20, 2013 10:08 PMModerator -
I got error at this line:
DataServiceResponse response = context.EndSaveChanges(result);
I can't understand the error. The Error is "The following error occured: <?xml version="1.0" encoding="utf-8"?><m:error xmlns:m="<m:code">http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><m:code /><m:message xml:lang="zh-CN">禁止</m:message></m:error>"
禁止 means forbidden.
王林
Saturday, September 21, 2013 1:55 AM -
hi, Eric
please help me or give me ways to update sqlserver database using WCF data Service.
Thanks a lot.
王林
Tuesday, September 24, 2013 1:02 PM -
The SOAP error message suggest that you do not have permission to insert items to the table/database.
If you setup the data service as described in How to: Create the Northwind Data Service ... then the "Orders" table probably does not have the EntitySetRights.WriteAppend permissions, which is required to add (append) items to the table.
-Eric.
- Marked as answer by wanglin005 Wednesday, September 25, 2013 11:20 AM
Tuesday, September 24, 2013 2:51 PMModerator -
Thank you.
王林
Wednesday, September 25, 2013 11:28 AM