0

I am trying to do CRUD operations in c# using Linq on Products of the NWTraders Database. While Adding a new product, I am trying to display Supplier Name and Category Name instead of the Supplier ID and Category ID(which are foreign keys of the Product table).

I have tried to Add new product and it crashes soon after I press OK to save it on to the database and update my data grid. But I noticed that the new product is getting updated into the database with supplier and Category IDs as Null which is further preventing me from accessing the Product Windows form itself as it is not able to retrieve the IDs of the corresponding Supplier and Category Names that I am giving to the new product during the ADD.

cmbSupplierName.SelectedIndex seem to receiving a NULL value and this.product.Supplier.Company is throwing the Null Reference Exception. Same is the problem with category. If I handle them with the if condition, then it still throws me an exception in the below code.

private void LoadProductInformation()
        {


            lblProductHeader.Text = "Information about :" + this.product.ProductName;
            txtProductID.Text = this.product.ProductID.ToString();
            txtProductName.Text = this.product.ProductName;
// Not loading for Add Products as User has to enter the values.
            if (this.Mode != ProductViewMode.Add)
            {
                cmbSupplierName.SelectedIndex = cmbSupplierName.FindString(this.product.Supplier.CompanyName);
                cmbCategory.SelectedIndex = cmbCategory.FindString(this.product.Category.CategoryName);


                txtQuantityPerUnit.Text = this.product.QuantityPerUnit;
                txtUnitPrice.Text = this.product.UnitPrice.Value.ToString("C");

                txtUnitsInStock.Text = this.product.UnitsInStock.Value.ToString();
                txtUnitsOnOrder.Text = this.product.UnitsOnOrder.Value.ToString();
                txtReorderLevel.Text = this.product.ReorderLevel.Value.ToString();


                chkDiscontinued.Checked = (this.product.Discontinued == true);
            }

        }

public void LoadDGVProducts(IEnumerable<Product> products)
        {
            // If there are no products, do nothing and return from the function.
            if (products == null) return;
            FetchData(); //fetching all the serach parameters 

            this.dgvProducts.SelectionChanged -= new System.EventHandler(this.DGVProducts_SelectionChanged);


            if (dgvProducts.RowCount == 0)
                FormatDGVProducts();

            dgvProducts.Rows.Clear();

            // Go through every product in the product collection and 
            // add it as a row in the dgv

                foreach (Product prod in products)
                {
                    dgvProducts.Rows.Add(
                        prod.ProductID, // The ID will not actually be shown since it is given to a column that has the Visible property set to False.
                        prod.ProductName,
                        prod.Supplier.CompanyName,
                        prod.Category.CategoryName,
                        prod.QuantityPerUnit,
                        prod.UnitPrice.Value.ToString("C"),
                        prod.UnitsInStock,
                        prod.UnitsOnOrder,
                        prod.ReorderLevel,
                        prod.Discontinued
                        );
                ...........................
            }
        }

Due to the supplier and category IDs receiving Null values on database, it is throwing me the exceptions at the 'foreach' as it won't let product to display on data grid if even one of the values in null in that condition.

I don't know where I am supposed to connect Supplier ID to Name for it to not receive Null values on the database.

Student
  • 11
  • 1
  • The only way to not recieve null values from the database is to not have null values in the database. You need null guards in your code. – fredrik Oct 07 '19 at 09:29
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Fildor Oct 07 '19 at 09:32

0 Answers0