private void LoadExistingProducts()
        {
            using (var db = new POSApplication.Model.posdbEntities())
            {
                string selectedCategoryName = ExistingCategoryList.GetItemText(ExistingCategoryList.SelectedItem);
                var    query = (from p in db.products
                                join c in db.categories on p.CategoryID equals c.CategoryID
                                //join s in db.suppliers on p.SupplierID equals s.SupplierID
                                where c.CategoryName == selectedCategoryName
                                select new { ProductName = p.ProductName, ProductDescription = p.ProductDescription, Barcode = p.Barcode, ImageFile = p.ImageFileName, SupplierID = p.SupplierID }).ToList();

                //ExistingProductList.Items.Clear();
                int a = 0;
                foreach (var item in query)
                {
                    if (a == 0)
                    {
                        var supp = (from s in db.suppliers
                                    where s.SupplierID == item.SupplierID
                                    select new { SupplierName = s.SupplierName }).SingleOrDefault();

                        //ExistingProductList.Text = item.ProductName;
                        //ExistingDescriptionField.Text = item.ProductDescription;
                        //if(supp != null)
                        //{
                        //    ExistingSupplierField.Text = supp.SupplierName;
                        //}
                        //else
                        //{
                        //    ExistingSupplierField.Text = "No Supplier";
                        //}
                        //ExistingBarcodeField.Text = item.Barcode;

                        //if(item.ImageFile != null)
                        //{
                        //    FileStream fs = new FileStream(item.ImageFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                        //    ExistingProductImage.Image = Image.FromStream(fs);
                        //    fs.Close();
                        //}
                    }
                    //ExistingProductList.Items.Add(item.ProductName);
                    a += 1;
                }
            }
        }
        private void ExistingCategoryList_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            var SelectedCategory = ExistingCategoryList.GetItemText(ExistingCategoryList.SelectedItem);

            using (var db = new POSApplication.Model.posdbEntities())
            {
                var query = (from d in db.products
                             join c in db.categories on
                             d.CategoryID equals c.CategoryID
                             where c.CategoryName == SelectedCategory
                             select new
                {
                    CategoryName = c.CategoryName,
                    ProductName = d.ProductName
                }).ToList();
                ExistingProductsList.Items.Clear();
                foreach (var item in query)
                {
                    ExistingProductsList.Items.Add(item.ProductName);
                }
            }
        }
 private void ExistingProductsList_DoubleClick(object sender, EventArgs e)
 {
     clearFields();
     using (var db = new POSApplication.Model.posdbEntities())
     {
         var ExistingProduct = ExistingProductsList.GetItemText(ExistingProductsList.SelectedItem);
         selectedProductName = ExistingProduct;
         var query = (from d in db.products
                      //join  s in db.suppliers on
                      //d.SupplierID equals s.SupplierID
                      where d.ProductName == ExistingProduct
                      select new
         {
             ProductName = d.ProductName,
             ProductDescription = d.ProductDescription,
             //SupplierName = s == null ? "No Supplier" : s.SupplierName,
             Barcode = d.Barcode,
             WarrantyYears = d.WarrantyPeriodYears,
             GuaranteeYears = d.GuaranteePeriodYears,
             ProductCode = d.ProductCode,
             Size = d.ProductSize,
             Width = d.ProductWidth,
             Height = d.ProductHeight,
             Weight = d.ProductWeight,
             Color = d.ProductColor,
             Image = d.ImageFileName
         }).SingleOrDefault();
         var supplierQuery = (from d in db.products
                              join s in db.suppliers on
                              d.SupplierID equals s.SupplierID
                              where d.ProductName == ExistingProduct
                              select new
         {
             SupplierName = s == null ? "No Supplier" : s.SupplierName
         }).SingleOrDefault();
         CategoryList.Text            = ExistingCategoryList.GetItemText(ExistingCategoryList.SelectedItem);
         ProductNameField.Text        = query.ProductName;
         ProductDescriptionField.Text = query.ProductDescription;
         ProductCodeField.Text        = query.ProductCode;
         if (supplierQuery != null)
         {
             SuppliersList.Text = supplierQuery.SupplierName;
         }
         else
         {
             SuppliersList.Text = "No Supplier";
         }
         ProductBarcodeField.Text = query.Barcode;
         WarrantyField.Text       = query.WarrantyYears;
         GuaranteeField.Text      = query.GuaranteeYears;
         SizeField.Text           = query.Size;
         WidthField.Text          = query.Width;
         WeightField.Text         = query.Weight;
         HeightField.Text         = query.Height;
         ColorField.Text          = query.Color;
         if (query.Image != null)
         {
             try
             {
                 string ImagesDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Images");
                 //Console.WriteLine(ImagesDirectory + "\\" + query.Image);
                 filename = ImagesDirectory + "\\" + query.Image;
                 FileStream fs = new FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                 ProductImage.Image = Image.FromStream(fs);
                 fs.Close();
             }
             catch
             {
             }
         }
     }
 }
        private void ExistingProductsList_DoubleClick(object sender, EventArgs e)
        {
            if (ExistingProductsList.SelectedItem != null)
            {
                clearFields();
                using (var db = new POSApplication.Model.posdbEntities())
                {
                    var ExistingProduct = ExistingProductsList.GetItemText(ExistingProductsList.SelectedItem);
                    int productid       = getProductID(ExistingProduct);
                    selectedProductName = ExistingProduct;
                    DateTime lowdate = DateTime.Parse("1111/11/11");
                    var      query   = (from d in db.inventories
                                        join p in db.products on
                                        d.ProductID equals p.ProductID
                                        join c in db.categories on
                                        p.CategoryID equals c.CategoryID
                                        where d.ProductID == productid && d.UpdateDt == null
                                        orderby d.PurchasePrice descending
                                        select new
                    {
                        CategoryName = c.CategoryName,
                        ProductName = p.ProductName,
                        Barcode = p.Barcode,
                        Quantity = d.SumQuantity,
                        //ReorderLevel = d.ReorderValue,
                        LastCostPrice = d.PurchasePrice,
                        SalePrice = d.SalePrice,
                        LastPurchaseDate = d.PurchaseDate,
                        Imagepath = p.ImageFileName
                    }).FirstOrDefault();
                    CategoryList.Text     = ExistingCategoryList.GetItemText(ExistingCategoryList.SelectedItem);
                    ProductNameField.Text = query.ProductName;
                    BarCodeField.Text     = query.Barcode;

                    var results =
                        (from item in db.inventories
                         where item.ProductID == productid && item.UpdateDt == null
                         group item by item.ProductID into g
                         select new { totalQuantity = g.Sum(item => item.SumQuantity) }).SingleOrDefault();

                    //QuantityField.Text = query.Quantity.ToString();
                    QuantityField.Text = results.totalQuantity.ToString();
                    //ReOrderLevelField.Text = query.ReorderLevel.ToString();
                    CostPriceField.Text    = query.LastCostPrice.ToString();
                    SalePriceField.Text    = query.SalePrice.ToString();
                    PurchaseDateField.Text = query.LastPurchaseDate.ToString();

                    if (query.Imagepath != null)
                    {
                        try
                        {
                            string ImagesDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Images");
                            Console.WriteLine(ImagesDirectory + "\\" + query.Imagepath);
                            FileStream fs = new FileStream(ImagesDirectory + "\\" + query.Imagepath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

                            ProductImage.Image = Image.FromStream(fs);
                            fs.Close();
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }