292 lines
13 KiB
C#
292 lines
13 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Drawing;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
using MySql.Data.MySqlClient;
|
||
|
||
namespace demMalyhin
|
||
{
|
||
public partial class AdmEditProduct : Form
|
||
{
|
||
private string productId;
|
||
|
||
private byte[] imageData = null;
|
||
|
||
public AdmEditProduct(string productId)
|
||
{
|
||
InitializeComponent();
|
||
LoadSuppliers();
|
||
LoadCategories();
|
||
LoadManufacturers();
|
||
LoadProductData(productId);
|
||
|
||
}
|
||
|
||
private void LoadProductData(string productId)
|
||
{
|
||
try
|
||
{
|
||
DB db = new DB();
|
||
using (MySqlConnection connection = db.getConnection())
|
||
{
|
||
connection.Open();
|
||
string query = "SELECT product_name, category_id, supplier_id, manufacturer_id, price, quantity_in_stock, description, unit_type, sale_percent, photo FROM products WHERE article_product = @id";
|
||
using (MySqlCommand command = new MySqlCommand(query, connection))
|
||
{
|
||
command.Parameters.AddWithValue("@id", productId);
|
||
using (MySqlDataReader reader = command.ExecuteReader())
|
||
{
|
||
if (reader.Read())
|
||
{
|
||
textBoxArticle.Text = productId;
|
||
textBoxName.Text = reader["product_name"].ToString();
|
||
comboBoxCategory.Text = reader["category_id"].ToString();
|
||
comboBoxSupplier.Text = reader["supplier_id"].ToString();
|
||
comboBoxManufacturer.Text = reader["manufacturer_id"].ToString();
|
||
textBoxPrice.Text = reader["price"].ToString();
|
||
textBoxQuantity.Text = reader["quantity_in_stock"].ToString();
|
||
textBoxDesc.Text = reader["description"].ToString();
|
||
textBoxUnit.Text = reader["unit_type"].ToString();
|
||
textBoxSale.Text = reader["sale_percent"].ToString();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Продукт с указанным ID не найден.");
|
||
this.DialogResult = DialogResult.Cancel;
|
||
this.Close();
|
||
}
|
||
if (reader["photo"] != DBNull.Value)
|
||
{
|
||
// Загружаем данные изображения из базы данных в класс-уровневую переменную imageData
|
||
this.imageData = (byte[])reader["photo"];
|
||
using (MemoryStream ms = new MemoryStream(this.imageData))
|
||
{
|
||
pictureBoxEditProduct.Image = Image.FromStream(ms);
|
||
pictureBoxEditProduct.SizeMode = PictureBoxSizeMode.Zoom;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
this.imageData = null; // Убеждаемся, что imageData обнуляется, если фото нет
|
||
pictureBoxEditProduct.Image = null; // Или устанавливаем стандартную заглушку
|
||
// pictureBoxEditProduct.Image = Properties.Resources.picture;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (MySqlException ex)
|
||
{
|
||
MessageBox.Show("Ошибка при загрузке данных продукта: " + ex.Message);
|
||
}
|
||
}
|
||
|
||
private void btnEditChooseImage_Click(object sender, EventArgs e)
|
||
{
|
||
openFileDialogImage.Filter = "Image Files (*.png;*.jpg;*.jpeg)|*.png;*.jpg;*.jpeg";
|
||
openFileDialogImage.Title = "Выберите новое изображение товара";
|
||
|
||
if (openFileDialogImage.ShowDialog() == DialogResult.OK)
|
||
{
|
||
try
|
||
{
|
||
string filePath = openFileDialogImage.FileName;
|
||
pictureBoxEditProduct.Image = Image.FromFile(filePath);
|
||
pictureBoxEditProduct.SizeMode = PictureBoxSizeMode.Zoom;
|
||
|
||
using (var ms = new MemoryStream())
|
||
{
|
||
using (Image originalImage = Image.FromFile(filePath))
|
||
{
|
||
originalImage.Save(ms, originalImage.RawFormat);
|
||
imageData = ms.ToArray();
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show("Ошибка при загрузке изображения: " + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
pictureBoxEditProduct.Image = null;
|
||
imageData = null;
|
||
}
|
||
}
|
||
}
|
||
|
||
private void btnEditRemoveImage_Click(object sender, EventArgs e)
|
||
{
|
||
pictureBoxEditProduct.Image = null;
|
||
imageData = null;
|
||
}
|
||
|
||
private void btnSave_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
|
||
object supplierId = comboBoxSupplier.SelectedValue;
|
||
object manufacturerId = comboBoxManufacturer.SelectedValue;
|
||
object categoryId = comboBoxCategory.SelectedValue;
|
||
|
||
DB db = new DB();
|
||
using (MySqlConnection connection = db.getConnection())
|
||
{
|
||
connection.Open();
|
||
string query = "UPDATE products SET product_name = @pr_name, category_id = @category_id, manufacturer_id = @manufacturer_id, unit_type = @unit, quantity_in_stock = @quantity, price = @price, supplier_id = @supplier_id, description = @desc, photo = @product_photo, sale_percent = @sale WHERE article_product = @id";
|
||
using (MySqlCommand command = new MySqlCommand(query, connection))
|
||
{
|
||
command.Parameters.AddWithValue("@id", textBoxArticle.Text);
|
||
command.Parameters.AddWithValue("@pr_name", textBoxName.Text);
|
||
command.Parameters.AddWithValue("@desc", textBoxDesc.Text);
|
||
command.Parameters.AddWithValue("@sale", textBoxSale.Text);
|
||
command.Parameters.AddWithValue("@unit", textBoxUnit.Text);
|
||
command.Parameters.AddWithValue("@quantity", textBoxQuantity.Text);
|
||
command.Parameters.AddWithValue("@price", Convert.ToDecimal(textBoxPrice.Text));
|
||
command.Parameters.AddWithValue("@supplier_id", supplierId);
|
||
command.Parameters.AddWithValue("@manufacturer_id", manufacturerId);
|
||
command.Parameters.AddWithValue("@category_id", categoryId);
|
||
|
||
if (imageData != null)
|
||
{
|
||
// Если изображение было выбрано или изменено
|
||
command.Parameters.Add(new MySqlParameter("@product_photo", MySqlDbType.MediumBlob) { Value = imageData });
|
||
}
|
||
else
|
||
{
|
||
// Если изображение удалено или не было выбрано
|
||
command.Parameters.Add(new MySqlParameter("@product_photo", MySqlDbType.MediumBlob) { Value = imageData });
|
||
}
|
||
|
||
int rowsAffected = command.ExecuteNonQuery();
|
||
if (rowsAffected > 0)
|
||
{
|
||
MessageBox.Show("Данные продукта успешно обновлены.");
|
||
this.DialogResult = DialogResult.OK;
|
||
this.Close();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось обновить данные продукта.");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (MySqlException ex)
|
||
{
|
||
MessageBox.Show("Ошибка при сохранении данных продукта: " + ex.Message);
|
||
}
|
||
}
|
||
|
||
private void LoadCategories()
|
||
{
|
||
try
|
||
{
|
||
DB db = new DB();
|
||
using (MySqlConnection connection = db.getConnection())
|
||
{
|
||
connection.Open();
|
||
string query = "SELECT id_Category, Category_name FROM Category";
|
||
using (MySqlCommand command = new MySqlCommand(query, connection))
|
||
{
|
||
using (MySqlDataReader reader = command.ExecuteReader())
|
||
{
|
||
DataTable categoryTable = new DataTable();
|
||
categoryTable.Load(reader);
|
||
|
||
comboBoxCategory.DataSource = categoryTable;
|
||
comboBoxCategory.DisplayMember = "Category_name";
|
||
comboBoxCategory.ValueMember = "id_Category";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (MySqlException ex)
|
||
{
|
||
MessageBox.Show("Ошибка при загрузке категорий: " + ex.Message);
|
||
}
|
||
}
|
||
|
||
private void LoadSuppliers()
|
||
{
|
||
try
|
||
{
|
||
DB db = new DB();
|
||
using (MySqlConnection connection = db.getConnection())
|
||
{
|
||
connection.Open();
|
||
string query = "SELECT id_Supplier, supplier_name FROM Supplier";
|
||
using (MySqlCommand command = new MySqlCommand(query, connection))
|
||
{
|
||
using (MySqlDataReader reader = command.ExecuteReader())
|
||
{
|
||
DataTable suppTable = new DataTable();
|
||
suppTable.Load(reader);
|
||
|
||
comboBoxSupplier.DataSource = suppTable;
|
||
comboBoxSupplier.DisplayMember = "supplier_name";
|
||
comboBoxSupplier.ValueMember = "id_Supplier";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (MySqlException ex)
|
||
{
|
||
MessageBox.Show("Ошибка при загрузке категорий: " + ex.Message);
|
||
}
|
||
}
|
||
|
||
private void LoadManufacturers()
|
||
{
|
||
try
|
||
{
|
||
DB db = new DB();
|
||
using (MySqlConnection connection = db.getConnection())
|
||
{
|
||
connection.Open();
|
||
string query = "SELECT id_Manufacturer, Manufacturer_name FROM Manufacturer";
|
||
using (MySqlCommand command = new MySqlCommand(query, connection))
|
||
{
|
||
using (MySqlDataReader reader = command.ExecuteReader())
|
||
{
|
||
DataTable manuTable = new DataTable();
|
||
manuTable.Load(reader);
|
||
|
||
comboBoxManufacturer.DataSource = manuTable;
|
||
comboBoxManufacturer.DisplayMember = "Manufacturer_name";
|
||
comboBoxManufacturer.ValueMember = "id_Manufacturer";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (MySqlException ex)
|
||
{
|
||
MessageBox.Show("Ошибка при загрузке категорий: " + ex.Message);
|
||
}
|
||
}
|
||
|
||
private void AdmEditProduct_Load(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void btnBack_Click(object sender, EventArgs e)
|
||
{
|
||
this.Close();
|
||
AdmMainForm amf = new AdmMainForm();
|
||
amf.Show();
|
||
}
|
||
|
||
private void btnExit_Click(object sender, EventArgs e)
|
||
{
|
||
Application.Exit();
|
||
}
|
||
}
|
||
|
||
|
||
}
|