0

I am encountering error that says:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

on the 3rd line of this Button's code:

ReadAllProductsList dbproduct = new ReadAllProductsList();
        DB_ProductsList = dbproduct.GetAllProducts();//Get all DB contacts    
        if (DB_ProductsList.Count > 0)
        {
            btnDelete.IsEnabled = true;
        }
        listBoxobj.ItemsSource = DB_ProductsList.OrderByDescending(i => i.Id).ToList();

I am not sure why this is happening. Is my database actually created?

When I remove the if statement and its body, I get another error that says:

Value cannot be null

on line

listBoxobj.ItemsSource = DB_ProductsList.OrderByDescending(i => i.Id).ToList();

Does it mean the table of database is not really there?

But on the adding new product form I don't get any error though, so I am guessing the DB is there.

1 Answers1

-1
listBoxobj.ItemsSource = DB_ProductsList.OrderByDescending(i => i.Id).ToList();

add this line of code within if statement.

ReadAllProductsList dbproduct = new ReadAllProductsList();
    DB_ProductsList = dbproduct.GetAllProducts();//Get all DB contacts    
    if (DB_ProductsList.Count > 0)
    {
        listBoxobj.ItemsSource = DB_ProductsList.OrderByDescending(i => i.Id).ToList();
        btnDelete.IsEnabled = true;
    }
Mohamathu Rafi
  • 115
  • 1
  • 2