private void ImportInDB(DataRowCollection firstTableRows, DateTime dateTimeOfReports)
        {
            using (NitrogenNewEntities dbConnection = new NitrogenNewEntities())
            {
                var nameOfPlace = firstTableRows[0].ItemArray[0].ToString();
                var idOfThePlace = dbConnection.Places.Where(p => p.Name == nameOfPlace).Select(p => p.PlaceId).FirstOrDefault();
                var idsOfProducts = dbConnection.Products.Select(x => x.ProductId).ToList();

                Place newPlace = new Place();
                if (idOfThePlace == 0)
                {
                    newPlace = dbConnection.Places.Add(new Place()
                    {
                        Name = nameOfPlace,
                        Address = "Undefined",
                    });
                }

                Sale theNewSale = new Sale();
                for (int i = 2; i < firstTableRows.Count; i++)
                {
                    theNewSale = new Sale();
                    var row = firstTableRows[i];
                    int reportProductId = int.Parse(row.ItemArray[0].ToString());

                    if (!idsOfProducts.Contains(reportProductId))
                    {
                        Console.WriteLine(
                            string.Format("The id: {0} is not contains in Products! Report cannot be processed.", reportProductId));
                        continue;
                    }

                    theNewSale.ProductId = reportProductId;
                    theNewSale.Quantity = int.Parse(row.ItemArray[1].ToString());
                    theNewSale.PricePerUnit = decimal.Parse(row.ItemArray[2].ToString());
                    theNewSale.Sum = decimal.Parse(row.ItemArray[3].ToString());
                    theNewSale.PlaceId = idOfThePlace != 0 ? idOfThePlace : newPlace.PlaceId;
                    theNewSale.Date = dateTimeOfReports;
                    dbConnection.Sales.Add(theNewSale);
                }

                try
                {
                    dbConnection.SaveChanges();
                }
                catch (Exception ex)
                {
                    this.ShowMessageException(ex);
                }
            }
        }
Esempio n. 2
0
        private void ImportInDB(DataRowCollection firstTableRows, DateTime dateTimeOfReports)
        {
            using (NitrogenNewEntities dbConnection = new NitrogenNewEntities())
            {
                var nameOfPlace   = firstTableRows[0].ItemArray[0].ToString();
                var idOfThePlace  = dbConnection.Places.Where(p => p.Name == nameOfPlace).Select(p => p.PlaceId).FirstOrDefault();
                var idsOfProducts = dbConnection.Products.Select(x => x.ProductId).ToList();

                Place newPlace = new Place();
                if (idOfThePlace == 0)
                {
                    newPlace = dbConnection.Places.Add(new Place()
                    {
                        Name    = nameOfPlace,
                        Address = "Undefined",
                    });
                }

                Sale theNewSale = new Sale();
                for (int i = 2; i < firstTableRows.Count; i++)
                {
                    theNewSale = new Sale();
                    var row             = firstTableRows[i];
                    int reportProductId = int.Parse(row.ItemArray[0].ToString());

                    if (!idsOfProducts.Contains(reportProductId))
                    {
                        Console.WriteLine(
                            string.Format("The id: {0} is not contains in Products! Report cannot be processed.", reportProductId));
                        continue;
                    }

                    theNewSale.ProductId    = reportProductId;
                    theNewSale.Quantity     = int.Parse(row.ItemArray[1].ToString());
                    theNewSale.PricePerUnit = decimal.Parse(row.ItemArray[2].ToString());
                    theNewSale.Sum          = decimal.Parse(row.ItemArray[3].ToString());
                    theNewSale.PlaceId      = idOfThePlace != 0 ? idOfThePlace : newPlace.PlaceId;
                    theNewSale.Date         = dateTimeOfReports;
                    dbConnection.Sales.Add(theNewSale);
                }

                try
                {
                    dbConnection.SaveChanges();
                }
                catch (Exception ex)
                {
                    this.ShowMessageException(ex);
                }
            }
        }