Beispiel #1
0
        private void init_product()
        {
            product = new Product();
            Additionally0 additionally0 = new Additionally0();
            Additionally  additionally  = new Additionally();
            ForName       forName       = new ForName();

            string[] values = lines[i].Split('|');
            for (int j = 0; j < values.Count(); j++)
            {
                if (headers[j].Equals("IMG_1"))
                {
                    Resource res = new Resource();
                    res.img1 = values[j++];
                    try
                    {
                        if (headers[j].Equals("IMG_2"))
                        {
                            res.img2 = values[j++];
                        }
                        if (headers[j].Equals("IMG_3"))
                        {
                            res.img3 = values[j++];
                        }
                        if (headers[j].Equals("IMG_4"))
                        {
                            res.img4 = values[j++];
                        }
                        if (headers[j].Equals("PDF_1"))
                        {
                            res.pdf = values[j++];
                        }
                        if (headers[j].Equals("Icon"))
                        {
                            res.pdf = values[j++];
                        }
                    }
                    catch (IndexOutOfRangeException e) { }
                    using (var db = new ProductDBEntitie())
                    {
                        db.Resource.Add(res);
                        db.SaveChanges();
                        additionally.Resource    = res;
                        additionally.id_resource = res.id;
                    }

                    continue;
                }


                if (headers[j] == "forname1")
                {
                    forName.forname1 = values[j];
                    forName.forname2 = values[j + 1];
                    using (var db = new ProductDBEntitie())
                    {
                        try
                        {
                            db.ForName.Add(forName);
                            db.SaveChanges();
                        }
                        catch
                        {
                            string str = values[j + 1];
                            forName = (from c in db.ForName where c.forname2.Equals(str) select c).FirstOrDefault();
                        }
                        additionally0.id_forname = forName.id;
                        additionally0.ForName    = (ForName)forName;
                    }
                    j++;
                }
                if (headers[j] == "forname2")
                {
                    j++;
                }
                try
                {
                    if (!product.init_product(headers[j], values[j], ref additionally0))
                    {
                        product.init_product(headers[j], values[j], ref additionally);
                    }
                }
                catch { }
            }
            using (var db = new ProductDBEntitie())
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        if (additionally0.ForName != null)
                        {
                            additionally0.ForName = null;
                            db.Additionally0.Add(additionally0);
                            db.SaveChanges();
                            product.id_additionally0 = additionally0.id;
                            product.Additionally0    = additionally0;
                        }
                    }
                    catch { }

                    try
                    {
                        if (additionally.Resource != null)
                        {
                            Resource res = additionally.Resource;
                            additionally.Resource = null;
                            db.Additionally.Add(additionally);
                            db.SaveChanges();
                            additionally.Resource   = res;
                            product.id_additionally = additionally.id;
                            product.Additionally    = additionally;
                        }
                    }
                    catch { }

                    try
                    {
                        BrandName drand = product.BrandName;
                        product.BrandName     = null;
                        product.Additionally  = null;
                        product.Additionally0 = null;
                        db.Product.Add(product);
                        db.SaveChanges();
                        product.BrandName     = drand;
                        additionally0.ForName = forName;
                        product.Additionally0 = additionally0;
                        product.Additionally  = additionally;
                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        product = null;
                        transaction.Rollback();
                    }
                }
            }
        }
        private void DataTable_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            TextBox t      = e.EditingElement as TextBox;
            String  value  = t.Text.ToString();
            String  header = e.Column.Header.ToString();
            var     sku    = (e.Row.DataContext as Product).sku;

            /// additionally additionally0
            ///
            if (sku != null)
            {
                using (var db = new ProductDBEntitie())
                {
                    var product = (from c in db.Product where c.sku == sku select c).FirstOrDefault();
                    if (product != null)
                    {
                        var additionally  = (from c in db.Additionally where c.id == product.id_additionally select c).FirstOrDefault();
                        var additionally0 = (from c in db.Additionally0 where c.id == product.id_additionally0 select c).FirstOrDefault();

                        if (additionally == null)
                        {
                            additionally = new Additionally();
                            db.Additionally.Add(additionally);
                            db.SaveChanges();
                            product.id_additionally = additionally.id;
                            db.SaveChanges();
                        }
                        Resource res = (from c in db.Resource where c.id == additionally.id_resource select c).FirstOrDefault();

                        if (header.Equals("IMG_1"))
                        {
                            res.img1 = value;
                        }
                        if (header.Equals("IMG_2"))
                        {
                            res.img2 = value;
                        }
                        if (header.Equals("IMG_3"))
                        {
                            res.img3 = value;
                        }
                        if (header.Equals("IMG_4"))
                        {
                            res.img4 = value;
                        }
                        if (header.Equals("PDF_1"))
                        {
                            res.pdf = value;
                        }
                        if (header.Equals("Icon"))
                        {
                            res.pdf = value;
                        }

                        db.Resource.Add(res);
                        db.SaveChanges();
                        additionally.Resource    = res;
                        additionally.id_resource = res.id;
                        db.SaveChanges();


                        if (additionally0 == null)
                        {
                            additionally0 = new Additionally0();
                            db.Additionally0.Add(additionally0);
                            db.SaveChanges();
                            product.id_additionally0 = additionally0.id;
                            db.SaveChanges();
                        }
                        var forName = (from c in db.ForName where c.id == additionally0.id_forname select c).FirstOrDefault();
                        if (header == "FORNAME1")
                        {
                            forName.forname1 = value;
                            try
                            {
                                db.ForName.Add(forName);
                                db.SaveChanges();
                            }
                            catch
                            {
                                string str = value;
                                forName = (from c in db.ForName where c.forname2.Equals(str) select c).FirstOrDefault();
                            }
                            additionally0.id_forname = forName.id;
                            additionally0.ForName    = (ForName)forName;
                            db.SaveChanges();
                            return;
                        }
                        if (header == "FORNAME2")
                        {
                            forName.forname2 = value;
                            try
                            {
                                db.ForName.Add(forName);
                                db.SaveChanges();
                            }
                            catch
                            {
                                string str = value;
                                forName = (from c in db.ForName where c.forname2.Equals(str) select c).FirstOrDefault();
                            }
                            additionally0.id_forname = forName.id;
                            additionally0.ForName    = (ForName)forName;
                            db.SaveChanges();
                            return;
                        }

                        try
                        {
                            if (!product.init_product(header, t.Text.ToString(), ref additionally0))
                            {
                                product.init_product(header, value, ref additionally);
                            }
                        }
                        catch (Exception ee) { MessageBox.Show(ee.Message); }
                        BrandName b = product.BrandName;
                        product.BrandName = null;
                        db.SaveChanges();
                        product.BrandName = b;
                    }
                }
            }
            else
            {
                SetSKU setSKU = new SetSKU(products);
                setSKU.ShowDialog();
            }
        }