0
votes

The problem:

Object reference not set to an instance of an object ( AFTER clicking an item in GridView the 2nd time).

This Object somehow refers to UoMCollection.Clear() and problem encountered here: txtBlkUoM.Text = Uom.UoM.ToString();

-1-- Declare this in the page

  ObservableCollection UoMCollection = new ObservableCollection();


-2--  Gridview :  Click an item in GridView, it will do the following:

 PopUpItem.IsOpen = true; ( this is the popUp Box ) 

 GetAllUoMForThisItem(No);


-2-- This will fill up another ListBox calls : lsBoxUoM

 private async void GetAllUoMForThisItem(string No)
        { 
            var db = new SQLiteAsyncConnection(DBPath);


       ( I need to clear the content each time an item selected in GridView )
            UoMCollection.Clear();   

            var allUoM = await db.QueryAsync("Select * From ItemUoM Where ItemNo = '" + No + "'"); 

            foreach (var _UoM in allUoM)
            {
                string strUoM = _UoM.UoM;

                AddToList(strUoM);
            }

            lsBoxUoM.ItemsSource = null;
            lsBoxUoM.ItemsSource = UoMCollection;

       }


 private void AddToList(string uom)
        {

            UoMCollection.Add(new ItemUoM()
            {
                UoM = uom
            });

        }




-3-- Make a selection in this PopUpBox

 private void lsBoxUoM_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

          if( lsBoxUoM.SelectedIndex == -1);

            var Uom = (ItemUoM)lsBoxUoM.SelectedItem;
            txtBlkUoM.Text = Uom.UoM.ToString();

        }


-4- Close this PopBox after a selection is made and Start from (2)  again


1

1 Answers

0
votes

In lsBoxUoM_SelectionChanged you test for lsBoxUoM.SelectedIndex == -1. This should be lsBoxUoM.SelectedIndex != -1. And after txtBlkUoM.Text = Uom.UoM.ToString(); you should reset the SelectedIndex to -1 in order to raise the selection changed event again when the user selects the same item again.