Advertisement
Guest User

Untitled

a guest
Aug 28th, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.00 KB | None | 0 0
  1. <DataGrid HorizontalAlignment="Left" Margin="10,21,0,0" VerticalAlignment="Top" RenderTransformOrigin="-18.493,-6.121" Height="288" Width="497"
  2. ItemsSource="{Binding ProductList, Mode=TwoWay}"
  3. AutoGenerateColumns="False"
  4. CanUserAddRows="True"
  5. CanUserDeleteRows="True">
  6. <DataGrid.Columns>
  7. <DataGridTextColumn Header="Product Name" Width="*" Binding="{Binding ProductName, Mode=TwoWay}"/>
  8. <DataGridTextColumn Header="Description" Width="*" Binding="{Binding Descriptiopn, Mode=TwoWay}"/>
  9. <DataGridTextColumn Header="Price" Width="*" Binding="{Binding Price, Mode=TwoWay}"/>
  10. </DataGrid.Columns>
  11. <i:Interaction.Triggers>
  12. <i:EventTrigger EventName="RowEditEnding">
  13. <Custom:EventToCommand PassEventArgsToCommand="True" Command="{Binding EditrowEvent}"/>
  14. </i:EventTrigger>
  15. </i:Interaction.Triggers>
  16. </DataGrid>
  17.  
  18. public MainViewModel()
  19. {
  20. if (!IsInDesignMode)
  21. {
  22. ProductList = new ObservableCollection<EntryViewModel>();
  23. dbContext = new DataProvider();
  24.  
  25. Thread UpdateGui = new Thread(UpdateteGui);
  26. UpdateGui.Start();
  27.  
  28. EditrowEvent = new RelayCommand<DataGridRowEditEndingEventArgs>(RowEditing);
  29. }
  30.  
  31. }
  32.  
  33. private void RowEditing(DataGridRowEditEndingEventArgs e)
  34. {
  35. if(e.EditAction == DataGridEditAction.Commit)
  36. {
  37. EntryViewModel emv = e.Row.DataContext as EntryViewModel;
  38.  
  39. Entry newEntry = new Entry
  40. {
  41. ProductName = emv.ProductName,
  42. Descriptiopn = emv.Descriptiopn,
  43. Price = emv.Price
  44.  
  45. };
  46.  
  47. dbContext.WriteToDB(newEntry);
  48. }
  49. }
  50.  
  51. public class DataProvider
  52. {
  53. ModelContext dbContext;
  54. public List<Entry> Allproducts()
  55. {
  56. using(dbContext = new ModelContext())
  57. {
  58. List<Entry> _entrydata = new List<Entry>();
  59.  
  60. var thdata = dbContext.Data;
  61.  
  62. foreach (var item in thdata)
  63. {
  64. _entrydata.Add(new Entry { Descriptiopn = item.Desc, Price = item.Pprice, ProductName = item.ProdName });
  65. }
  66. return _entrydata;
  67. }
  68.  
  69. }
  70.  
  71. public bool WriteToDB(Entry entry)
  72. {
  73. try
  74. {
  75. dbContext.Data.Add(new Data { ProdName = entry.ProductName, Desc = entry.Descriptiopn, Pprice = entry.Price, Id = Guid.NewGuid() });
  76. dbContext.SaveChanges();
  77. }
  78. catch (Exception)
  79. {
  80. return false;
  81. }
  82. return true;
  83. }
  84. }
  85.  
  86. System.Reflection.TargetInvocationException was unhandled
  87. _HResult=-2146232828
  88. _message=Exception has been thrown by the target of an invocation.
  89. HResult=-2146232828
  90. IsTransient=false
  91. Message=Exception has been thrown by the target of an invocation.
  92. Source=mscorlib
  93. StackTrace:
  94. at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
  95. at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
  96. at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  97. at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
  98. at GalaSoft.MvvmLight.Helpers.WeakAction`1.Execute(T parameter) in c:MvvmLightSourceGalaSoft.MvvmLightGalaSoft.MvvmLight (PCL)HelpersWeakActionGeneric.cs:line 204
  99. at GalaSoft.MvvmLight.CommandWpf.RelayCommand`1.Execute(Object parameter) in c:MvvmLightSourceGalaSoft.MvvmLightGalaSoft.MvvmLight (PCL)CommandRelayCommandGeneric.cs:line 232
  100. at GalaSoft.MvvmLight.Command.EventToCommand.Invoke(Object parameter) in c:MvvmLightSourceGalaSoft.MvvmLightGalaSoft.MvvmLight.Platform (NET45)CommandEventToCommand.cs:line 338
  101. at System.Windows.Interactivity.TriggerBase.InvokeActions(Object parameter)
  102. at System.Windows.Interactivity.EventTriggerBase.OnEvent(EventArgs eventArgs)
  103. at System.Windows.Interactivity.EventTriggerBase.OnEventImpl(Object sender, EventArgs eventArgs)
  104. at System.Windows.Controls.DataGrid.OnRowEditEnding(DataGridRowEditEndingEventArgs e)
  105. at System.Windows.Controls.DataGrid.OnExecutedCommitEdit(ExecutedRoutedEventArgs e)
  106. at System.Windows.Controls.DataGrid.OnExecutedCommitEdit(Object sender, ExecutedRoutedEventArgs e)
  107. at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
  108. at System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding)
  109. at System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
  110. at System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
  111. at System.Windows.UIElement.OnExecutedThunk(Object sender, ExecutedRoutedEventArgs e)
  112. at System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(Delegate genericHandler, Object target)
  113. at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
  114. at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
  115. at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
  116. at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
  117. at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
  118. at System.Windows.Input.RoutedCommand.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated)
  119. at System.Windows.Input.RoutedCommand.Execute(Object parameter, IInputElement target)
  120. at System.Windows.Controls.DataGrid.EndEdit(RoutedCommand command, DataGridCell cellContainer, DataGridEditingUnit editingUnit, Boolean exitEditMode)
  121. at System.Windows.Controls.DataGrid.OnCurrentCellChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  122. at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
  123. at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
  124. at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
  125. at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
  126. at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
  127. at System.Windows.DependencyObject.SetCurrentValueInternal(DependencyProperty dp, Object value)
  128. at System.Windows.Controls.DataGrid.set_CurrentCellContainer(DataGridCell value)
  129. at System.Windows.Controls.DataGrid.set_FocusedCell(DataGridCell value)
  130. at System.Windows.Controls.DataGridCell.OnAnyGotFocus(Object sender, RoutedEventArgs e)
  131. at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
  132. at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
  133. at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
  134. at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
  135. at System.Windows.FrameworkElement.OnGotFocus(RoutedEventArgs e)
  136. at System.Windows.UIElement.IsFocused_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  137. at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
  138. at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
  139. at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
  140. at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
  141. at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
  142. at System.Windows.DependencyObject.SetValue(DependencyPropertyKey key, Object value)
  143. at System.Windows.Input.FocusManager.OnFocusedElementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  144. at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
  145. at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
  146. at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
  147. at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
  148. at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
  149. at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
  150. at System.Windows.Input.FocusManager.SetFocusedElement(DependencyObject element, IInputElement value)
  151. at System.Windows.Input.KeyboardNavigation.UpdateFocusedElement(DependencyObject focusTarget)
  152. at System.Windows.FrameworkElement.OnGotKeyboardFocus(Object sender, KeyboardFocusChangedEventArgs e)
  153. at System.Windows.Input.KeyboardFocusChangedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
  154. at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
  155. at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
  156. at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
  157. at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
  158. at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
  159. at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
  160. at System.Windows.Input.InputManager.ProcessStagingArea()
  161. at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
  162. at System.Windows.Input.KeyboardDevice.ChangeFocus(DependencyObject focus, Int32 timestamp)
  163. at System.Windows.Input.KeyboardDevice.TryChangeFocus(DependencyObject newFocus, IKeyboardInputProvider keyboardInputProvider, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
  164. at System.Windows.Input.KeyboardDevice.Focus(DependencyObject focus, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
  165. at System.Windows.Input.KeyboardDevice.Focus(IInputElement element)
  166. at System.Windows.UIElement.Focus()
  167. at System.Windows.Input.KeyboardNavigation.Navigate(DependencyObject currentElement, TraversalRequest request, ModifierKeys modifierKeys, DependencyObject firstElement)
  168. at System.Windows.FrameworkElement.MoveFocus(TraversalRequest request)
  169. at System.Windows.Controls.DataGrid.OnTabKeyDown(KeyEventArgs e)
  170. at System.Windows.Controls.DataGrid.OnKeyDown(KeyEventArgs e)
  171. at System.Windows.UIElement.OnKeyDownThunk(Object sender, KeyEventArgs e)
  172. at System.Windows.Input.KeyEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
  173. at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
  174. at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
  175. at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
  176. at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
  177. at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
  178. at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
  179. at System.Windows.Input.InputManager.ProcessStagingArea()
  180. at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
  181. at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
  182. at System.Windows.Interop.HwndKeyboardInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawKeyboardActions actions, Int32 scanCode, Boolean isExtendedKey, Boolean isSystemKey, Int32 virtualKey)
  183. at System.Windows.Interop.HwndKeyboardInputProvider.ProcessKeyAction(MSG& msg, Boolean& handled)
  184. at System.Windows.Interop.HwndSource.CriticalTranslateAccelerator(MSG& msg, ModifierKeys modifiers)
  185. at System.Windows.Interop.HwndSource.OnPreprocessMessage(Object param)
  186. at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
  187. at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
  188. at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
  189. at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
  190. at System.Windows.Interop.HwndSource.OnPreprocessMessageThunk(MSG& msg, Boolean& handled)
  191. at System.Windows.Interop.HwndSource.WeakEventPreprocessMessage.OnPreprocessMessage(MSG& msg, Boolean& handled)
  192. at System.Windows.Interop.ComponentDispatcherThread.RaiseThreadMessage(MSG& msg)
  193. at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
  194. at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
  195. at System.Windows.Threading.Dispatcher.Run()
  196. at System.Windows.Application.RunDispatcher(Object ignore)
  197. at System.Windows.Application.RunInternal(Window window)
  198. at System.Windows.Application.Run(Window window)
  199. at System.Windows.Application.Run()
  200. at WPFClient.App.Main() in c:UsersVavyDocumentsVisual Studio 2013ProjectsWPFMSMQDatagridToDataBaseWPFClientobjDebugApp.g.cs:line 0
  201. at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
  202. at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
  203. at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
  204. at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
  205. at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
  206. at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
  207. at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  208. at System.Threading.ThreadHelper.ThreadStart()
  209. InnerException: System.NullReferenceException
  210. _HResult=-2147467261
  211. _message=Object reference not set to an instance of an object.
  212. HResult=-2147467261
  213. IsTransient=false
  214. Message=Object reference not set to an instance of an object.
  215. Source=WPFClient
  216. StackTrace:
  217. at WPFClient.ViewModel.MainViewModel.RowEditing(DataGridRowEditEndingEventArgs e) in c:UsersVavyDocumentsVisual Studio 2013ProjectsWPFMSMQDatagridToDataBaseWPFClientViewModelMainViewModel.cs:line 60
  218. InnerException:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement