locked
Binding a Listview to a SQLite using a LINQ query.

    Question

  • I have a function that queries a SQLite DB and pulls in data:

    private void pullPendingOrders()
            {
                
                
                    var orderPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "orders.db");
                    using (var db = new SQLite.SQLiteConnection(orderPath))
                    {
                                            
                        
                        var pendingOrderListQuery = (from c in db.Table<OrderHeader>()
                                                  where c.orderStatus.Equals("2")
                                                  select c).OrderByDescending(x => x.orderNumber);
                        
    
                        lstPendingOrders.Items.Clear();
    
                        lstPendingOrders.ItemsSource = pendingOrderListQuery;                    
    
                        db.Dispose();
                        db.Close();
    
                    }
    
    
                
            }

    When it runs, it throws a Null exception on '

    lstPendingOrders.Items.Clear();

    and 

    lstPendingOrders.ItemsSource = pendingOrderListQuery; 

    What am I doing wrong?

    Tuesday, October 14, 2014 7:17 PM

Answers

  • I fixed my issued

    I had class load as:

    public HDWOrderList()
            {
                pullPendingOrders();
                this.InitializeComponent();
                
            }

    It should have been:

    public HDWOrderList()
            {
                
                this.InitializeComponent();
                pullPendingOrders();
            }

    Tuesday, October 14, 2014 8:42 PM
  • Hi,

                        var pendingOrderListQuery = (from c in db.Table<OrderHeader>()

                                                  where c.orderStatus.Equals("2")
                                                  select c).OrderByDescending(x => x.orderNumber);

                        lstPendingOrders.Items.Clear();

                        foreach (OrderHeader orderHeader in pendingOrderListQuery)
                        {
                              lstPendingOrders.Add(orderHeader );
                        }

     with ...

            private ObservableCollection<OrderHeader > _ lstPendingOrders = new ObservableCollection<OrderHeader >();
            public ObservableCollection<OrderHeader > lstPendingOrders
            {
                get { return _ lstPendingOrders; }
                set { _ lstPendingOrders = value; }
            }

    and bind the collection to the listview in xaml

    Regards

    Torsten


    Thursday, October 16, 2014 1:28 PM

All replies

  • I fixed my issued

    I had class load as:

    public HDWOrderList()
            {
                pullPendingOrders();
                this.InitializeComponent();
                
            }

    It should have been:

    public HDWOrderList()
            {
                
                this.InitializeComponent();
                pullPendingOrders();
            }

    Tuesday, October 14, 2014 8:42 PM
  • Hi GarlingBeard,

    Thank you for sharing your solution here.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 16, 2014 1:23 AM
    Moderator
  • Hi,

                        var pendingOrderListQuery = (from c in db.Table<OrderHeader>()

                                                  where c.orderStatus.Equals("2")
                                                  select c).OrderByDescending(x => x.orderNumber);

                        lstPendingOrders.Items.Clear();

                        foreach (OrderHeader orderHeader in pendingOrderListQuery)
                        {
                              lstPendingOrders.Add(orderHeader );
                        }

     with ...

            private ObservableCollection<OrderHeader > _ lstPendingOrders = new ObservableCollection<OrderHeader >();
            public ObservableCollection<OrderHeader > lstPendingOrders
            {
                get { return _ lstPendingOrders; }
                set { _ lstPendingOrders = value; }
            }

    and bind the collection to the listview in xaml

    Regards

    Torsten


    Thursday, October 16, 2014 1:28 PM