I have an ObservableCollection that when i load data into it one time and when i load again it thorws an exception the object is not set on reference
using (PricingManagerEntities db = new PricingManagerEntities())
{
m_PricingSheetManagerViewModel.Groups = new ObservableCollection<sp_GetGroups_Result>(db.sp_GetGroups(null, true));
}
return m_PricingSheetManagerViewModel;
I did the follwing:
spilt the line into servel lines:
var a = db.sp_GetGroups(null, true).ToList(); b = new ObservableCollection<sp_GetGroups_Result>(a); m_PricingSheetManagerViewModel.Groups = b;
and notice that it throws when: m_PricingSheetManagerViewModel.Groups = b;
I tried to clear the Groups try to remove the items, but i always getting System.NullReferenceException
the first time I initialize the Groups the Groups is null, and getting 2 records then i select one of the groups via combobox and exit the window, next i enter again and it throws me.
here's the combobox xaml:
<ComboBox HorizontalAlignment="Left" Width="200" Name="cmbGroups" ItemsSource="{Binding Groups}" Height="30" DisplayMemberPath="GroupName" SelectionChanged="cmbGroup_changed" HorizontalContentAlignment="Center" Margin="5"/>
here's when selection changed:
private void cmbGroup_changed(object sender, SelectionChangedEventArgs e)
{
long groupID = ((sender as ComboBox).SelectedValue as sp_GetGroups_Result).GroupID;
m_engine.GetItemsByGroupID(groupID);
}
internal void GetItemsByGroupID(long groupID)
{
if (m_PricingSheetManagerViewModel.Items == null)
m_PricingSheetManagerViewModel.Items = new ObservableCollection<sp_GetItems_Result>();
m_PricingSheetManagerViewModel.Items.Clear();
using (PricingManagerEntities db = new PricingManagerEntities())
{
List<ItemsInGroups> groups = db.ItemsInGroups.Where(iig => iig.GroupID == groupID).ToList();
foreach(ItemsInGroups group in groups)
{
var temp = db.sp_GetItems(group.ItemID, true).FirstOrDefault();
if(temp != null)
m_PricingSheetManagerViewModel.Items.Add(temp);
}
}
}