There is a WPF form containing 8 textboxes.
<StackPanel Grid.Column="2">
<TextBox Margin="5" Width="100" Text="{Binding SelectedOrder.OrderCode}" FontSize="16" FontWeight="Normal" FontStyle="Normal"/>
<TextBox Margin="10" Width="100" Text="{Binding SelectedOrder.ClientID}" FontSize="16" FontWeight="Normal" FontStyle="Normal"/>
<TextBox Margin="10" Width="100" Text="{Binding SelectedOrder.RouteCode}" FontSize="16" FontWeight="Normal" FontStyle="Normal"/>
<TextBox Margin="10" Width="100" Text="{Binding SelectedOrder.DriverID}" FontSize="16" FontWeight="Normal" FontStyle="Normal"/>
<TextBox Margin="10" Width="100" Text="{Binding SelectedOrder.TCCode}" FontSize="16" FontWeight="Normal" FontStyle="Normal"/>
<TextBox Margin="10" Width="100" Text="{Binding SelectedOrder.Date}" FontSize="16" FontWeight="Normal" FontStyle="Normal"/>
<TextBox Margin="10" Width="100" Text="{Binding SelectedOrder.StartDate}" FontSize="16" FontWeight="Normal" FontStyle="Normal"/>
<TextBox Margin="10" Width="100" Text="{Binding SelectedOrder.EndDate}" FontSize="16" FontWeight="Normal" FontStyle="Normal"/>
<Button Margin="50" Width="100" Content="Create"
Command="{Binding AddCommand}"
/>
</StackPanel>
There is a ViewModel class that implements the transfer of data from the view to the model
public class OrderViewModel : INotifyPropertyChanged
{
private ObservableCollection<Order> orderList; //list of records in the Order table
private Order selectedOrder; // specific entry in Order
private TransportCompanyEntities transportCompanyEntities; // context?
public Order SelectedOrder
{
get { return selectedOrder; }
set
{
selectedOrder = value;
OnPropertyChanged(nameof(SelectedOrder));
}
}
public ObservableCollection<Order> OrderList
{
get { return orderList; }
set
{
orderList = value;
OnPropertyChanged(nameof(OrderList));
}
}
public OrderViewModel()
{
transportCompanyEntities = new TransportCompanyEntities();
Load Orders();
}
private void LoadOrders()
{
OrderList = new ObservableCollection<Order>(transportCompanyEntities.Order);
}
private RelayCommand addCommand;
public RelayCommand AddCommand
{
get
{
return addCommand ??
(addCommand = new RelayCommand(obj =>
{
transportCompanyEntities.Order.Add(SelectedOrder);
transportCompanyEntities.SaveChanges();
}));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class RelayCommand{}
When the button is clicked, an entry should be created, but an error occurs
How can i fix it?
System.ArgumentNullException: "Value cannot be null. Parameter name: entity"
I understand that I am trying to add an empty entry, but it is not clear to me why it is not created when the button is clicked.