Try this:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if( !cbStatus.IsLoaded ) return;
... then your code ...
}
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi guys if i uncomment code below it will error with System.NullReferenceException: 'Object reference not set to an instance of an object.' lvOrders was null . If i comment its ok , i started debug and every thing run ok and i check if i selectedItem.Content the it error . I just start learning i hope u guys can teach me thank you !
=== my code :
using Microsoft.IdentityModel.Tokens;
using MiYu.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MiYu
{
/// <summary>
/// Interaction logic for ManageOrders.xaml
/// </summary>
public partial class ManageOrders : UserControl
{
public ManageOrders()
{
InitializeComponent();
}
public void Load(string a,string b)
{
int staId = 5;
if(b.Equals("Cancel")) { staId = 5; }
if (b.Equals("Done")) { staId = 3; }
var ctOrders = MiYuContext.INSTANCE.Orders.Where(x => x.StatusId == staId).Select(x => new
{
Id = x.Id,
TimeStart = x.TimeStart,
Price = x.Price,
EmployeeName = MiYuContext.INSTANCE.Accounts.Where(y => y.Id.Equals(x.EmployeeId)).Select(x => x.Name).FirstOrDefault(),
status = "Processing",
CustomerName = "Guest",
TableId = x.TableId,
TableName = x.Table.Name,
Floor = x.Table.Floor,
}).ToList();
if (!a.IsNullOrEmpty())
{
ctOrders = MiYuContext.INSTANCE.Orders.Where(x => x.StatusId == staId && x.Table.Name.Contains(a)).Select(x => new
{
Id = x.Id,
TimeStart = x.TimeStart,
Price = x.Price,
EmployeeName = MiYuContext.INSTANCE.Accounts.Where(y => y.Id.Equals(x.EmployeeId)).Select(x => x.Name).FirstOrDefault(),
status = "Processing",
CustomerName = "Guest",
TableId = x.TableId,
TableName = x.Table.Name,
Floor = x.Table.Floor,
}).ToList();
}
lvOrders.ItemsSource = ctOrders;
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Load("","");
}
private void txFindTable_TextChanged(object sender, TextChangedEventArgs e)
{
Load(txFindTable.Text, "");
}
private void search_Click(object sender, RoutedEventArgs e)
{
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedItem = cbStatus.SelectedItem as ComboBoxItem;
/* if (selectedItem.Content.ToString().Equals("Processing")) { Load(txFindTable.Text, "Processing"); }
if (selectedItem.Content.ToString().Equals("Cancel")) { Load(txFindTable.Text, "Cancel"); }
if (selectedItem.Content.ToString().Equals("Done")) { Load(txFindTable.Text, "Done"); }*/
}
}
}
Try this:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if( !cbStatus.IsLoaded ) return;
... then your code ...
}
I see no code to initial lvOrders, so it’s probably null. While not your current issue,
if (!a.IsNullOrEmpty())
Will fail if a is null. Try
if (!string.IsNullOrEmpty(a))