demMalyhinMerkulov/EditProductForm.cs
2025-11-25 12:59:48 +04:00

144 lines
5.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using MySql.Data.MySqlClient;
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 System.Windows.Forms.VisualStyles;
namespace megadem
{
public partial class EditProductForm : Form
{
private string productID;
public EditProductForm(string productID)
{
InitializeComponent();
LoadTypes();
LoadProductData(productID);
}
private void LoadProductData(string productId)
{
try
{
DB db = new DB();
using (MySqlConnection connection = db.getConnection())
{
connection.Open();
string query = "SELECT Product_name, Product_type_id, Min_price_partner, Width FROM Product WHERE Article = @id";
using (MySqlCommand command = new MySqlCommand(query, connection))
{
command.Parameters.AddWithValue("@id", productId);
using (MySqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
Article.Text = productId;
NameProd.Text = reader["product_name"].ToString();
comboBoxType.Text = reader["Product_type_id"].ToString();
MinPrice.Text = reader["Min_price_partner"].ToString();
Width.Text = reader["Width"].ToString();
}
else
{
MessageBox.Show("Продукт с указанным ID не найден.");
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}
}
}
}
catch (MySqlException ex)
{
MessageBox.Show("Ошибка при загрузке данных продукта: " + ex.Message);
}
}
private void LoadTypes()
{
try
{
DB db = new DB();
using (MySqlConnection connection = db.getConnection())
{
connection.Open();
string query = "SELECT idProduct_Type, ProductType FROM Product_type";
using (MySqlCommand command = new MySqlCommand(query, connection))
{
using (MySqlDataReader reader = command.ExecuteReader())
{
DataTable typeTable = new DataTable();
typeTable.Load(reader);
comboBoxType.DataSource = typeTable;
comboBoxType.DisplayMember = "ProductType";
comboBoxType.ValueMember = "idProduct_Type";
}
}
}
}
catch (MySqlException ex)
{
MessageBox.Show("Ошибка при загрузке категорий: " + ex.Message);
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
object typeId = comboBoxType.SelectedValue;
DB db = new DB();
using (MySqlConnection connection = db.getConnection())
{
connection.Open();
string query = "UPDATE Product SET Product_name = @pr_name, Product_type_id = @type, Min_price_partner = @price, Width = @width WHERE Article = @id";
using (MySqlCommand command = new MySqlCommand(query, connection))
{
command.Parameters.AddWithValue("@id", Article.Text);
command.Parameters.AddWithValue("@width", Convert.ToDecimal(Width.Text));
command.Parameters.AddWithValue("@pr_name", NameProd.Text);
command.Parameters.AddWithValue("@type", typeId);
command.Parameters.AddWithValue("@price", Convert.ToDecimal(MinPrice.Text));
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected > 0)
{
MessageBox.Show("Данные продукта успешно обновлены.");
this.DialogResult = DialogResult.OK;
this.Hide();
MainForm form = new MainForm();
form.Show();
}
else
{
MessageBox.Show("Не удалось обновить данные продукта.");
}
}
}
}
catch (MySqlException ex)
{
MessageBox.Show("Ошибка при сохранении данных продукта: " + ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
MainForm mainForm = new MainForm();
mainForm.Show();
}
}
}