172 lines
6.8 KiB
C#
172 lines
6.8 KiB
C#
using MySql.Data.MySqlClient;
|
||
using NeshinaPolina2111;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Diagnostics;
|
||
using System.Drawing;
|
||
using System.Drawing.Imaging;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
using System.Xml.Linq;
|
||
|
||
namespace NeshinaPolina2111
|
||
{
|
||
public partial class AddMaterials : Form
|
||
{
|
||
public AddMaterials()
|
||
{
|
||
InitializeComponent();
|
||
LoadTypes();
|
||
LoadUnits();
|
||
}
|
||
|
||
private void AddMaterials_Load(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void btnAdd_Click(object sender, EventArgs e)
|
||
{ //ПРОВЕРКИ
|
||
if (string.IsNullOrWhiteSpace(txtName.Text))
|
||
{
|
||
MessageBox.Show("Введите название материала.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||
return;
|
||
}
|
||
|
||
if (cmbType.SelectedValue == null || cmbUnit.SelectedValue == null)
|
||
{
|
||
MessageBox.Show("Выберите тип материала и единицу измерения.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||
return;
|
||
}
|
||
|
||
decimal price;
|
||
int quantity, minQuantity, quantityPack;
|
||
|
||
if (!decimal.TryParse(txtPrice.Text, out price) || price < 0)
|
||
{
|
||
MessageBox.Show("Цена должна быть числом ≥ 0.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||
return;
|
||
}
|
||
|
||
if (!int.TryParse(txtQuantity.Text, out quantity) || quantity < 0)
|
||
{
|
||
MessageBox.Show("Количество должно быть целым числом ≥ 0.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||
return;
|
||
}
|
||
|
||
|
||
if (!int.TryParse(txtMinQuantity.Text, out minQuantity) || minQuantity< 0)
|
||
{
|
||
MessageBox.Show("Минимальное количество должно быть целым числом ≥ 0.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||
return;
|
||
}
|
||
|
||
if (!int.TryParse(txtQuantityPack.Text, out quantityPack) || quantityPack< 0)
|
||
{
|
||
MessageBox.Show("Количество в упаковке должно быть целым числом ≥ 0.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||
return;
|
||
}
|
||
//ВСТАВИТЬ В БД
|
||
string insertQuery = @"
|
||
INSERT INTO Materials_import (
|
||
Name,
|
||
idMaterial_type_import,
|
||
PriceUnitMaterial,
|
||
Quantity,
|
||
MinQuantity,
|
||
QuantityPack,
|
||
idUnit
|
||
) VALUES (
|
||
@Name,
|
||
@idMaterial_type_import,
|
||
@PriceUnitMaterial,
|
||
@Quantity,
|
||
@MinQuantity,
|
||
@QuantityPack,
|
||
@idUnit
|
||
)";
|
||
|
||
var conn = DB.GetInstance().GetConnection();
|
||
if (conn.State == ConnectionState.Closed)
|
||
conn.Open();
|
||
|
||
using (var cmd = new MySqlCommand(insertQuery, conn))
|
||
{
|
||
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
|
||
cmd.Parameters.AddWithValue("@idMaterial_type_import", cmbType.SelectedValue);
|
||
cmd.Parameters.AddWithValue("@PriceUnitMaterial", price);
|
||
cmd.Parameters.AddWithValue("@Quantity", quantity);
|
||
cmd.Parameters.AddWithValue("@MinQuantity", minQuantity);
|
||
cmd.Parameters.AddWithValue("@QuantityPack", quantityPack);
|
||
cmd.Parameters.AddWithValue("@idUnit", cmbUnit.SelectedValue);
|
||
|
||
try
|
||
{
|
||
int rowsAffected = cmd.ExecuteNonQuery();
|
||
if (rowsAffected > 0)
|
||
{
|
||
MessageBox.Show("Материал успешно добавлен!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
DialogResult = DialogResult.OK;
|
||
Close();
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("Не удалось добавить материал.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"Ошибка при добавлении: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
|
||
private void LoadTypes() //ЗАГРУЗКА ТИПОВ В КОМБО
|
||
{
|
||
string query = "SELECT idMaterial_type_import, Name FROM Material_type_import ORDER BY Name";
|
||
var conn = DB.GetInstance().GetConnection();
|
||
if (conn.State == ConnectionState.Closed)
|
||
conn.Open();
|
||
|
||
using (var cmd = new MySqlCommand(query, conn))
|
||
{
|
||
var adapter = new MySqlDataAdapter(cmd);
|
||
DataTable dt = new DataTable();
|
||
adapter.Fill(dt);
|
||
|
||
cmbType.DisplayMember = "Name";
|
||
cmbType.ValueMember = "idMaterial_type_import";
|
||
cmbType.DataSource = dt;
|
||
}
|
||
}
|
||
|
||
private void LoadUnits() //В КОМБОБОКС ЕДИНИЦЫ ЗАГРУЗИТЬ
|
||
{
|
||
string query = "SELECT idUnit, Name FROM Unit ORDER BY Name";
|
||
var conn = DB.GetInstance().GetConnection();
|
||
if (conn.State == ConnectionState.Closed)
|
||
conn.Open();
|
||
|
||
using (var cmd = new MySqlCommand(query, conn))
|
||
{
|
||
var adapter = new MySqlDataAdapter(cmd);
|
||
DataTable dt = new DataTable();
|
||
adapter.Fill(dt);
|
||
|
||
cmbUnit.DisplayMember = "Name";
|
||
cmbUnit.ValueMember = "idUnit";
|
||
cmbUnit.DataSource = dt;
|
||
}
|
||
}
|
||
|
||
private void btnExit_Click(object sender, EventArgs e)
|
||
{
|
||
this.Hide();
|
||
}
|
||
}
|
||
}
|