using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace WinFormsApp1 { public partial class Reduct2 : Form { public Reduct2(string productId) { InitializeComponent(); LoadProductData(productId); LoadCategories(); LoadSuppliers(); LoadManufacteries(); LoadUnitType(); } private void LoadCategories() { DB db = new DB(); DataTable categories = new DataTable(); MySqlDataAdapter adapter = new MySqlDataAdapter(); MySqlCommand command = new MySqlCommand("SELECT idCategory, CategoryName FROM Category", db.getConnection()); try { db.openConnection(); adapter.SelectCommand = command; adapter.Fill(categories); comboBoxCategories.DataSource = categories; comboBoxCategories.DisplayMember = "CategoryName"; comboBoxCategories.ValueMember = "idCategory"; } catch (Exception ex) { MessageBox.Show("Ошибка загрузки категорий " + ex.Message); } finally { db.closeConnection(); } } private void LoadManufacteries() { DB db = new DB(); DataTable manufacturer = new DataTable(); MySqlDataAdapter adapter = new MySqlDataAdapter(); MySqlCommand command = new MySqlCommand("SELECT idManufacturer, ManufacturerName FROM Manufacturer", db.getConnection()); try { db.openConnection(); adapter.SelectCommand = command; adapter.Fill(manufacturer); comboBoxManufacturer.DataSource = manufacturer; comboBoxManufacturer.DisplayMember = "ManufacturerName"; comboBoxManufacturer.ValueMember = "idManufacturer"; } catch (Exception ex) { MessageBox.Show("Ошибка загрузки категорий " + ex.Message); } finally { db.closeConnection(); } } private void LoadUnitType() { DB db = new DB(); DataTable UnitType = new DataTable(); MySqlDataAdapter adapter = new MySqlDataAdapter(); MySqlCommand command = new MySqlCommand("SELECT idUnitType, UnitTypeName FROM UnitType", db.getConnection()); try { db.openConnection(); adapter.SelectCommand = command; adapter.Fill(UnitType); comboBoxUnitType.DataSource = UnitType; comboBoxUnitType.DisplayMember = "UnitTypeName"; comboBoxUnitType.ValueMember = "idUnitType"; } catch (Exception ex) { MessageBox.Show("Ошибка загрузки категорий " + ex.Message); } finally { db.closeConnection(); } } private void LoadSuppliers() { DB db = new DB(); DataTable suppliers = new DataTable(); MySqlDataAdapter adapter = new MySqlDataAdapter(); MySqlCommand command = new MySqlCommand("SELECT idSupplier, SupplierName FROM Supplier", db.getConnection()); try { db.openConnection(); adapter.SelectCommand = command; adapter.Fill(suppliers); comboBoxSupplier.DataSource = suppliers; comboBoxSupplier.DisplayMember = "SupplierName"; comboBoxSupplier.ValueMember = "idSupplier"; } catch (Exception ex) { MessageBox.Show("Ошибка загрузки категорий " + ex.Message); } finally { db.closeConnection(); } } private void LoadProductData(string productId) { try { DB db = new DB(); using (MySqlConnection connection = db.getConnection()) { connection.Open(); string query = "SELECT Article, ProductName, Category_id, ProductDescryption, Manufacturer_id, Supplier_id, Price, Discount, QuantityStock, UnitType_id FROM Products WHERE idProducts = @id"; using (MySqlCommand command = new MySqlCommand(query, connection)) { command.Parameters.AddWithValue("@id", productId); using (MySqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { textBoxArticle.Text = reader["Article"].ToString(); textBoxID.Text = productId; textBoxProdName.Text = reader["ProductName"].ToString(); comboBoxCategories.Text = reader["Category_id"].ToString(); textBoxDesc.Text = reader["ProductDescryption"].ToString(); comboBoxManufacturer.Text = reader["Manufacturer_id"].ToString(); comboBoxSupplier.Text = reader["Supplier_id"].ToString(); textBoxPrice.Text = reader["Price"].ToString(); textBoxSale.Text = reader["Discount"].ToString(); textBoxStockQuan.Text = reader["QuantityStock"].ToString(); comboBoxUnitType.Text = reader["UnitType_id"].ToString(); } else { MessageBox.Show("Продукт с указанным ID не найден."); this.DialogResult = DialogResult.Cancel; this.Close(); } } } } } catch (MySqlException ex) { MessageBox.Show("Ошибка при загрузке данных продукта: " + ex.Message); } } private void btnSave_Click(object sender, EventArgs e) { try { object supplierId = comboBoxSupplier.SelectedValue; object manufacturerId = comboBoxManufacturer.SelectedValue; object categoryId = comboBoxCategories.SelectedValue; object unittypeId = comboBoxUnitType.SelectedValue; DB db = new DB(); using (MySqlConnection connection = db.getConnection()) { connection.Open(); string query = "UPDATE Products SET Article = @art, ProductName = @pr_name, Category_id = @cat, ProductDescryption = @desc, Manufacturer_id = @manu, Supplier_id = @supp, Price = @base, Discount = @sale, QuantityStock = @stock, UnitType_id = @unit WHERE idProducts = @id"; using (MySqlCommand command = new MySqlCommand(query, connection)) { command.Parameters.AddWithValue("@art", textBoxArticle.Text); command.Parameters.AddWithValue("@pr_name", textBoxProdName.Text); command.Parameters.AddWithValue("@desc", textBoxDesc.Text); command.Parameters.AddWithValue("@sale", textBoxSale.Text); command.Parameters.AddWithValue("@id", textBoxID.Text); command.Parameters.AddWithValue("@stock", textBoxStockQuan.Text); command.Parameters.AddWithValue("@base", textBoxPrice.Text); command.Parameters.AddWithValue("@supp", supplierId); command.Parameters.AddWithValue("@manu", manufacturerId); command.Parameters.AddWithValue("@cat", categoryId); command.Parameters.AddWithValue("@unit", unittypeId); int rowsAffected = command.ExecuteNonQuery(); if (rowsAffected > 0) { MessageBox.Show("Данные продукта успешно обновлены."); this.DialogResult = DialogResult.OK; this.Hide(); AdmMainForm main = new AdmMainForm(); main.Show(); } } } } catch (MySqlException ex) { MessageBox.Show("Ошибка при сохранении данных продукта: " + ex.Message); } } private void button2_Click(object sender, EventArgs e) { this.Hide(); AdmMainForm mainForm = new AdmMainForm(); mainForm.Show(); } private void button3_Click(object sender, EventArgs e) { this.Hide(); AdmMainForm af = new AdmMainForm(); af.Show(); } } }