185 lines
7.0 KiB
C#
185 lines
7.0 KiB
C#
using MySql.Data.MySqlClient;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using System.Xml.Linq;
|
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
|
|
|
namespace NeshinaPolina07_11
|
|
{
|
|
public partial class EditProduct : Form
|
|
{
|
|
private readonly int _productId;
|
|
private readonly MySqlConnection _connection;
|
|
public EditProduct(int productId)
|
|
{
|
|
InitializeComponent();
|
|
_productId = productId;
|
|
_connection = DB.GetInstance().GetConnection();
|
|
LoadComboBoxes();
|
|
LoadProductData();
|
|
}
|
|
|
|
private void LoadComboBoxes()
|
|
{
|
|
LoadComboBox("categories", "category_id", "category_name", cmbCategory);
|
|
LoadComboBox("manufacturers", "manufacturer_id", "manufacturer_name", cmbManufacturer);
|
|
LoadComboBox("suppliers", "supplier_id", "supplier_name", cmbSupplier);
|
|
}
|
|
|
|
private void LoadComboBox(string table, string idColumn, string nameColumn, System.Windows.Forms.ComboBox combo)
|
|
{
|
|
string query = $"SELECT {idColumn}, {nameColumn} FROM {table} ORDER BY {nameColumn}";
|
|
using (var cmd = new MySqlCommand(query, _connection))
|
|
using (var adapter = new MySqlDataAdapter(cmd))
|
|
{
|
|
var dt = new DataTable();
|
|
adapter.Fill(dt);
|
|
combo.DataSource = dt;
|
|
combo.DisplayMember = nameColumn;
|
|
combo.ValueMember = idColumn;
|
|
combo.SelectedIndex = -1;
|
|
}
|
|
}
|
|
|
|
private void LoadProductData()
|
|
{
|
|
string query = @"
|
|
SELECT
|
|
product_article, product_name, price, discount,
|
|
quantity_in_stock, description,
|
|
category_id, manufacturer_id, supplier_id
|
|
FROM products
|
|
WHERE product_id = @id";
|
|
|
|
using (var cmd = new MySqlCommand(query, _connection))
|
|
{
|
|
cmd.Parameters.AddWithValue("@id", _productId);
|
|
|
|
using (var reader = cmd.ExecuteReader())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
textBox1.Text = reader["product_article"].ToString();
|
|
textBox2.Text = reader["product_name"].ToString();
|
|
textBox3.Text = reader["price"].ToString();
|
|
textBox4.Text = reader["discount"].ToString();
|
|
textBox5.Text = reader["quantity_in_stock"].ToString();
|
|
textBox6.Text = reader["description"].ToString();
|
|
|
|
SetComboBoxValue(cmbCategory, reader["category_id"]);
|
|
SetComboBoxValue(cmbManufacturer, reader["manufacturer_id"]);
|
|
SetComboBoxValue(cmbSupplier, reader["supplier_id"]);
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Продукт не найден.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private void SetComboBoxValue(System.Windows.Forms.ComboBox combo, object value)
|
|
{
|
|
if (value != DBNull.Value && combo.DataSource != null)
|
|
{
|
|
foreach (DataRowView row in combo.Items)
|
|
{
|
|
if (row[combo.ValueMember].Equals(value))
|
|
{
|
|
combo.SelectedItem = row;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void EditProduct_Load(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void textBox1_TextChanged(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
{
|
|
if (!decimal.TryParse(textBox3.Text, out decimal price) || price < 0)
|
|
{
|
|
MessageBox.Show("Цена должна быть числом >= 0.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
if (!int.TryParse(textBox4.Text, out int discount) || discount < 0 || discount > 100)
|
|
{
|
|
MessageBox.Show("Скидка должна быть от 0 до 100%.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
if (!int.TryParse(textBox5.Text, out int quantity) || quantity < 0)
|
|
{
|
|
MessageBox.Show("Количество должно быть целым числом >= 0.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
string query = @"
|
|
UPDATE products
|
|
SET
|
|
product_article = @article,
|
|
product_name = @name,
|
|
unit = 'шт.', -- ← напрямую в запросе!
|
|
price = @price,
|
|
discount = @discount,
|
|
quantity_in_stock = @quantity,
|
|
description = @description,
|
|
category_id = @category_id,
|
|
manufacturer_id = @manufacturer_id,
|
|
supplier_id = @supplier_id
|
|
WHERE product_id = @id";
|
|
|
|
using (var cmd = new MySqlCommand(query, _connection))
|
|
{
|
|
cmd.Parameters.AddWithValue("@article", textBox1.Text);
|
|
cmd.Parameters.AddWithValue("@name", textBox2.Text);
|
|
cmd.Parameters.AddWithValue("@price", price);
|
|
cmd.Parameters.AddWithValue("@discount", discount);
|
|
cmd.Parameters.AddWithValue("@quantity", quantity);
|
|
cmd.Parameters.AddWithValue("@description", textBox6.Text);
|
|
cmd.Parameters.AddWithValue("@category_id", cmbCategory.SelectedValue ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@manufacturer_id", cmbManufacturer.SelectedValue ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@supplier_id", cmbSupplier.SelectedValue ?? DBNull.Value);
|
|
cmd.Parameters.AddWithValue("@id", _productId);
|
|
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
MessageBox.Show("Данные успешно обновлены.", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
|
|
private void cmbManufacturer_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void cmbSupplier_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
}
|