Welsh King
Welsh King

Reputation: 3238

Create WPFon showdialog event

I am creating a new window popup window using

PopupWindows.PaymentsSummary paymentsSummary = new PopupWindows.PaymentsSummary  
paymentsSummary.ParentWindow = Window.GetWindow(this);
paymentsSummary.ShowDialog();

on my load function in the Payment summary window I have

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        basepage.payments.BindPaymentSummaryToDataGrid(uiActiveItems, basepage.user.terminal.TerminalId, true);
        basepage.payments.BindPaymentSummaryToDataGrid(uiInActiveItems, basepage.user.terminal.TerminalId, false);
    }

The function is

    public void BindPaymentSummaryToDataGrid(DataGrid dgrid, int terminalId, bool isActivePayment)
    {
        BLPinNumber pins = new BLPinNumber();
        string pinNumber = String.Empty;
        long pinId = pins.getPinId(terminalId, ref pinNumber);
        using (var dbEntities = new DatabaseAccess.Schema.Entities())
        {
              dgrid.DataContext = dbEntities.getPaymentRecordsByPinId((int)pinId, isActivePayment);
        }
    }

The above code calls a Stored Proc in SQL Server and returns an object,

However when the app runs I get the error when clicking to show the popup on the following line paymentsSummary.ShowDialog();

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

I have worked that down to the following code in the XAML for the datagrid

DataGrid  ItemsSource="{Binding}" Grid.Column="{Binding}"

If i remove this code it works but the data doesnt load obvioulsy.

So what I believe I need to do is bind the datagrid onShowDialog method.

How do i create this ?

Or is there a better way of doing this using the Entity framework, im used to ASP.NET where working with DATAGRIDS seem easier, if ablight less powerful.

Many thanks

Upvotes: 1

Views: 154

Answers (1)

Chen Kinnrot
Chen Kinnrot

Reputation: 21015

Your problem is lazy loading!, you got 2 options:

  1. select the data with eager loading (change the getPaymentRecordsByPinId).
  2. do not dispose the dbEntities while popup is open.

Upvotes: 1

Related Questions