/Visual Studio 2008/CSADONETDataServiceSL3Client/SampleServiceInsert.xaml.cs

# · C# · 172 lines · 112 code · 16 blank · 44 comment · 5 complexity · 8934ac7ab002a96293eaab134f47903b MD5 · raw file

  1. /****************************** Module Header ******************************\
  2. * Module Name: SampleServiceInsert.xaml.cs
  3. * Project: CSADONETDataServiceSL3Client
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * SampleServiceInsert.cs demonstrates how to call ASP.NET Data Service
  7. * to select and insert records in Silverlight.
  8. *
  9. * This source is subject to the Microsoft Public License.
  10. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  11. * All other rights reserved.
  12. *
  13. * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  14. * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  16. \***************************************************************************/
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Net;
  21. using System.Windows;
  22. using System.Windows.Controls;
  23. using System.Windows.Documents;
  24. using System.Windows.Input;
  25. using System.Windows.Media;
  26. using System.Windows.Media.Animation;
  27. using System.Windows.Shapes;
  28. using CSADONETDataServiceSL3Client.SampleService;
  29. using System.Data.Services.Client;
  30. namespace CSADONETDataServiceSL3Client
  31. {
  32. public partial class SampleServiceInsert : UserControl
  33. {
  34. // The URL of ADO.NET Data Service
  35. private const string _sampleUri =
  36. "http://localhost:8888/Samples.svc";
  37. // returnedCategory => _context ={via async REST call}=> ADO.NET Data Service
  38. private SampleProjects _context;
  39. public SampleServiceInsert()
  40. {
  41. InitializeComponent();
  42. this.Loaded += new RoutedEventHandler(SampleServiceInsert_Loaded);
  43. }
  44. void SampleServiceInsert_Loaded(object sender, RoutedEventArgs e)
  45. {
  46. LoadData();
  47. }
  48. /// <summary>
  49. /// In this method we send a REST request to ADO.NET Data Service to request Category
  50. /// records.
  51. /// </summary>
  52. private void LoadData()
  53. {
  54. _context = new SampleProjects(new Uri(_sampleUri));
  55. DataServiceQuery<Category> query = (DataServiceQuery<Category>)(
  56. from c in _context.Categories
  57. select c);
  58. query.BeginExecute(OnCategoryQueryComplete, query);
  59. }
  60. /// <summary>
  61. /// Callback method of the query to get Category records.
  62. /// </summary>
  63. /// <param name="result"></param>
  64. private void OnCategoryQueryComplete(IAsyncResult result)
  65. {
  66. Dispatcher.BeginInvoke(() =>
  67. {
  68. DataServiceQuery<Category> query =
  69. result.AsyncState as DataServiceQuery<Category>;
  70. try
  71. {
  72. var returnedCategory =
  73. query.EndExecute(result);
  74. if (returnedCategory != null)
  75. {
  76. this.mainDataGrid.ItemsSource = returnedCategory.ToList();
  77. }
  78. }
  79. catch (DataServiceQueryException ex)
  80. {
  81. this.messageTextBlock.Text = string.Format("Error: {0} - {1}",
  82. ex.Response.StatusCode.ToString(), ex.Response.Error.Message);
  83. }
  84. });
  85. }
  86. /// <summary>
  87. /// In this event handler, we begin to send a REST request to ADO.NET
  88. /// Data Service to insert a new Category.
  89. /// </summary>
  90. /// <param name="sender"></param>
  91. /// <param name="e"></param>
  92. private void InsertButton_Click(object sender, RoutedEventArgs e)
  93. {
  94. string categorynameforinsert = this.categoryTextBox.Text;
  95. _context.AddToCategories(new Category() { CategoryName = categorynameforinsert });
  96. _context.BeginSaveChanges(OnChangesSaved, _context);
  97. }
  98. /// <summary>
  99. /// Callback method of the insert operation.
  100. /// </summary>
  101. /// <param name="result"></param>
  102. private void OnChangesSaved(IAsyncResult result)
  103. {
  104. Dispatcher.BeginInvoke(() =>
  105. {
  106. SampleProjects svcContext = result.AsyncState as SampleProjects;
  107. try
  108. {
  109. // Complete the save changes operation and display the response.
  110. WriteOperationResponse(svcContext.EndSaveChanges(result));
  111. }
  112. catch (DataServiceRequestException ex)
  113. {
  114. // Display the error from the response.
  115. WriteOperationResponse(ex.Response);
  116. }
  117. catch (InvalidOperationException ex)
  118. {
  119. messageTextBlock.Text = ex.Message;
  120. }
  121. finally
  122. {
  123. // Reload the binding collection to refresh Grid to see if it's successfully updated.
  124. // You can also remove the following line. To see the update effect, just refresh the page or check out database directly.
  125. LoadData();
  126. }
  127. });
  128. }
  129. /// <summary>
  130. /// This method shows details information of the response from ADO.NET Data Service.
  131. /// </summary>
  132. /// <param name="response"></param>
  133. private void WriteOperationResponse(DataServiceResponse response)
  134. {
  135. messageTextBlock.Text = string.Empty;
  136. int i = 1;
  137. if (response.IsBatchResponse)
  138. {
  139. messageTextBlock.Text = string.Format("Batch operation response code: {0}\n",
  140. response.BatchStatusCode);
  141. }
  142. foreach (ChangeOperationResponse change in response)
  143. {
  144. messageTextBlock.Text +=
  145. string.Format("\tChange {0} code: {1}\n",
  146. i.ToString(), change.StatusCode.ToString());
  147. if (change.Error != null)
  148. {
  149. string.Format("\tChange {0} error: {1}\n",
  150. i.ToString(), change.Error.Message);
  151. }
  152. i++;
  153. }
  154. }
  155. }
  156. }