public static List <ProductShop> LoadProductShopFromSql()
        {
            List <ProductShop> listProducts = new List <ProductShop>();

            using (var conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                string     sql = @"select * from Products";
                SqlCommand com = new SqlCommand(sql, conn);
                using (SqlDataReader rdr = com.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        ProductShop pr = new ProductShop();
                        pr.ID        = Int32.Parse(rdr[0].ToString());
                        pr.Name      = rdr[1].ToString();
                        pr.PriceBuy  = float.Parse(rdr[2].ToString());
                        pr.PriceSell = float.Parse(rdr[3].ToString());
                        pr.UoM       = (UoM)Enum.Parse(typeof(UoM), rdr[4].ToString());
                        listProducts.Add(pr);
                    }
                }
            }
            return(listProducts);
        }
 public static void SaveProductShopToXML(ProductShop product)
 {
     LoadProductShopFromXML();
     if (Products.Any(it => it.ID == product.ID))
     {
         Products.First(it => it.ID == product.ID).Name      = product.Name;
         Products.First(it => it.ID == product.ID).PriceBuy  = product.PriceBuy;
         Products.First(it => it.ID == product.ID).PriceSell = product.PriceSell;
         Products.First(it => it.ID == product.ID).UoM       = product.UoM;
     }
     else
     {
         product.ID = Products.Last().ID + 1;
         Products.Add(product);
     }
     try
     {
         var xmlReader = new XmlSerializer(typeof(List <ProductShop>));
         using (var stream = File.Create(PathFileProducts))
         {
             xmlReader.Serialize(stream, Products);
         }
     }
     catch (Exception)
     {
         MessageBox.Show("Ошибка записи списка пользователей", "ошибка", MessageBoxButtons.OK,
                         MessageBoxIcon.Error);
     }
 }
 private void button1_Click(object sender, EventArgs e)
 {
     if (textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "" || comboBox1.Text == "" || comboBox2.Text == "")
     {
         MessageBox.Show("Вы не заполнили некоторые поля", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
         return;
     }
     // it need to check name
     Products       = BridgeToBD.LoadProductShopFromDB(TypeBD.SQL);
     ProductsMoving = BridgeToBD.LoadProductMovingFromDB(TypeBD.SQL);
     if (!Products.Any(it => it.Name == textBox3.Text))
     {
         ProductShop pr = new ProductShop();
         pr.ID        = Products.Last().ID + 1;
         pr.Name      = textBox3.Text;
         pr.PriceSell = (float)Convert.ToDouble(textBox5.Text);
         pr.PriceBuy  = (float)Convert.ToDouble(textBox4.Text);
         pr.UoM       = (UoM)Enum.Parse(typeof(UoM), comboBox1.Text);
         ProductMoving prmove = new ProductMoving();
         prmove.IDproduct = pr.ID;
         prmove.Sold      = 0;
         if (comboBox2.Text == "Warehouse")
         {
             prmove.CountStore = Convert.ToDouble(textBox6.Text);
         }
         else if (comboBox2.Text == "Shop")
         {
             prmove.CountShop = Convert.ToDouble(textBox6.Text);
         }
         else
         {
             MessageBox.Show("Некорректно выбран склад");
         }
         Products.Add(pr);
         ProductsMoving.Add(prmove);
         //HelperProduct.SaveUsersFromFile(Products);
         // this.Update(); // it doesn't work
         //HelperProduct.InsertToSql(pr);
         BridgeToBD.SaveProductShopToBD(Products, TypeBD.SQL);
         BridgeToBD.SaveProductMovingToSQL(ProductsMoving);
         dataGridView1ReLoad();
     }
     else
     {
         MessageBox.Show("Товар с таким именем уже есть на складе");
     }
 }
 public static void SaveProductShopToSQL(ProductShop prShop)
 {
     using (var conn = new SqlConnection(ConnectionString))
     {
         LoadProductShopFromSql();
         conn.Open();
         SqlCommand comm = conn.CreateCommand();
         comm.CommandType = CommandType.StoredProcedure;
         comm.Parameters.Add("@id", SqlDbType.Int).Value           = prShop.ID;
         comm.Parameters.Add("@name", SqlDbType.NVarChar).Value    = prShop.Name;
         comm.Parameters.Add("@price_buy", SqlDbType.Money).Value  = prShop.PriceBuy;
         comm.Parameters.Add("@price_sell", SqlDbType.Money).Value = prShop.PriceSell;
         comm.Parameters.Add("@uom", SqlDbType.NVarChar).Value     = prShop.UoM;
         comm.CommandText = (Products.Any(it => it.ID == prShop.ID)) ? "ai_update_products" : "ai_insert_products";
         comm.ExecuteNonQuery();
     }
 }
 public static void SaveProductShopToEntity(ProductShop prShop)
 {
     LoadProductShopFromEntity();
     EntityLibrary.Products products = new EntityLibrary.Products();
     products.id        = prShop.ID;
     products.Name      = prShop.Name;
     products.PriceBuy  = (decimal)prShop.PriceBuy;
     products.PriceSell = (decimal)prShop.PriceSell;
     products.UoM       = prShop.UoM.ToString();
     if (Products.Any(it => it.ID == prShop.ID))
     {
         UpdateProductsToEntity(products);
     }
     else
     {
         InsertProductsToEntity(products);
     }
 }
        public static void SaveProductShopToBD(ProductShop prShop, TypeBD typeBD)
        {
            switch (typeBD)
            {
            case TypeBD.XML:
                SaveProductShopToXML(prShop);
                break;

            case TypeBD.SQL:
                SaveProductShopToSQL(prShop);
                break;

            case TypeBD.Entity:
                SaveProductShopToEntity(prShop);
                break;

            default:
                MessageBox.Show("ERROR typeBD");
                break;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox3.Text == string.Empty)
            {
                MessageBox.Show("Вы не заполнили поле 'Название товара'", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (comboBox1.Text == string.Empty)
            {
                MessageBox.Show("Вы не заполнили поле 'Единицы измерения'", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (textBox4.Text == "")
            {
                MessageBox.Show("Вы не заполнили поле 'Цена закупки'", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (textBox5.Text == "")
            {
                MessageBox.Show("Вы не заполнили поле 'Цена продажи'", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (textBox6.Text == "")
            {
                MessageBox.Show("Вы не заполнили поле 'Количество товара'", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (comboBox2.Text == "")
            {
                MessageBox.Show("Вы не заполнили поле 'Склад'", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            BridgeToBD.LoadProductShopFromDB(BridgeToBD.ChoiceBD);
            if (!BridgeToBD.Products.Any(it => it.Name == textBox3.Text))
            {
                ProductShop pr = new ProductShop();
                pr.Name      = textBox3.Text;
                pr.PriceSell = (float)Convert.ToDouble(textBox5.Text);
                pr.PriceBuy  = (float)Convert.ToDouble(textBox4.Text);
                pr.UoM       = (UoM)Enum.Parse(typeof(UoM), comboBox1.Text);

                ProductMoving prmove = new ProductMoving();
                prmove.Sold = 0;
                if (comboBox2.Text == "Warehouse")
                {
                    prmove.CountStore = Convert.ToDouble(textBox6.Text);
                    prmove.CountShop  = 0;
                }
                else if (comboBox2.Text == "Shop")
                {
                    prmove.CountShop  = Convert.ToDouble(textBox6.Text);
                    prmove.CountStore = 0;
                }
                else
                {
                    MessageBox.Show("Некорректно выбран склад");
                }
                BridgeToBD.SaveProductShopToBD(pr, BridgeToBD.ChoiceBD);
                BridgeToBD.LoadProductShopFromDB(BridgeToBD.ChoiceBD);
                prmove.IDproduct = BridgeToBD.Products.Last().ID;
                BridgeToBD.SaveProductMovingToBD(prmove, BridgeToBD.ChoiceBD);
                dataGridView1ReLoad();
            }
            else
            {
                MessageBox.Show("Товар с таким именем уже есть на складе");
            }
        }