0

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.

mixqq
  • 1
  • 1
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Sir Rufo Mar 22 '22 at 07:13
  • The error seems to be because `SelectedOrder` is null. Your example code does not show any means of guaranteeing that that cannot happen. – DonBoitnott Mar 22 '22 at 12:06
  • @mixqq: *Where* are you getting the exception in your code? – mm8 Mar 22 '22 at 13:26

0 Answers0