I have a picture that everything was working yesterday, but I also remember this error coming up before. I have surrounded:
this.MainDataTable.DefaultView.RowFilter =
$"YRNRO LIKE '{this.YRNROSearchKey}*'" +
$"AND HAKUNIMI LIKE '{this.HAKUNIMISearchKey}*'" +
$"AND KONSERNI LIKE '{this.GROUPSearchKey}*'" +
$"AND LY LIKE '{this.BUSINESSIDSearchKey}*'" +
FIANDSEBoolquery + ACTIVEBoolquery;
With try
& catch
and it was working, but now I am getting the same error again, even with try
& catch
at the place.
I have WPF DataGrid (MVVM) with data in and CheckBoxes used as filters. If I comment out setting CheckBoxes "checked" during load time, everything works fine. However setting them to true
is giving an error.
MainWindow.xaml.cs:
public MainWindow()
{
InitializeComponent();
_ = CheckForVPNConnectionTimeToTime();
NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;
var isAvailable = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
OnNetworkAvailabilityChanged(isAvailable);
// Commenting two lines below help to avoid errors
// How to get it working with setup like this
// when CheckBoxes are checked by default?
ActiveCustomer.IsChecked = true;
OnlyFIandSE.IsChecked = true;
}
ViewModel_Main.cs:
using System.ComponentModel;
using System.Data;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace Inspector_FilterTest
{
class ViewModel_Main : INotifyPropertyChanged
{
public ICommand LoadMainTableDataCommand => new RelayCommand(async param => await ExecuteLoadMainTableDataAsync());
private DataTable mainDataTable;
public DataTable MainDataTable
{
get => this.mainDataTable;
set
{
this.mainDataTable = value;
OnPropertyChanged();
// Set the DataTable filter expression
EnableRowFiltering();
}
}
...
// Binding checkbox FIANDSE Bool
private bool _FIANDSEBool;
public bool FIANDSEBool
{
get => this._FIANDSEBool;
set
{
this._FIANDSEBool = value;
OnPropertyChanged();
// Refresh the DataTable filter expression
EnableRowFiltering();
}
}
// Binding checkbox ACTIVE Bool
private bool _ACTIVEBool;
public bool ACTIVEBool
{
get => this._ACTIVEBool;
set
{
this._ACTIVEBool = value;
OnPropertyChanged();
// Refresh the DataTable filter expression
EnableRowFiltering();
}
}
...
public void EnableRowFiltering()
{
string FIANDSEBoolquery = FIANDSEBool ? " AND (YRNRO LIKE '6*' OR YRNRO LIKE '7*')" : string.Empty;
string ACTIVEBoolquery = ACTIVEBool ? " AND KAYTOSSA='1'" : string.Empty;
try
{
this.MainDataTable.DefaultView.RowFilter =
$"YRNRO LIKE '{this.YRNROSearchKey}*'" +
$"AND HAKUNIMI LIKE '{this.HAKUNIMISearchKey}*'" +
$"AND KONSERNI LIKE '{this.GROUPSearchKey}*'" +
$"AND LY LIKE '{this.BUSINESSIDSearchKey}*'" +
FIANDSEBoolquery + ACTIVEBoolquery;
}
catch (System.Exception)
{
// do nothing, just avoid application crash
}
}
public async Task<DataTable> LoadMainTableDataAsync()
{
return await Task.Run(() =>
{
if (MainProcess.Customers.Rows.Count > 1)
{
return MainProcess.Customers;
}
else
{
MainProcess.MergedTable();
return MainProcess.Customers;
}
});
}
private async Task ExecuteLoadMainTableDataAsync()
{
if (MainProcess.CheckForVPNInterface())
{
this.HasProgress = true;
this.MainDataTable = await LoadMainTableDataAsync();
this.HasProgress = false;
}
else
{
string caption = "VPN connection missing";
MessageBox.Show("Please, check your VPN connection!", caption,
MessageBoxButton.OK,
MessageBoxImage.Exclamation);
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
=> this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Error I am getting:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Inspector_FilterTest.ViewModel_Main.MainDataTable.get returned null.
Pointing at:
this.MainDataTable.DefaultView.RowFilter =
$"YRNRO LIKE '{this.YRNROSearchKey}*'" +
$"AND HAKUNIMI LIKE '{this.HAKUNIMISearchKey}*'" +
$"AND KONSERNI LIKE '{this.GROUPSearchKey}*'" +
$"AND LY LIKE '{this.BUSINESSIDSearchKey}*'" +
FIANDSEBoolquery + ACTIVEBoolquery;