156 lines
5.6 KiB
C#
156 lines
5.6 KiB
C#
using MySql.Data.MySqlClient;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
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 AddProduction : Form
|
||
{
|
||
public AddProduction()
|
||
{
|
||
InitializeComponent();
|
||
LoadTypes(); //загрузка типов
|
||
}
|
||
|
||
private void btnAdd_Click(object sender, EventArgs e)
|
||
{
|
||
//НАЧАЛО ВСЕХ ПРОВЕРОК
|
||
if (string.IsNullOrEmpty(tbArticle.Text))
|
||
{
|
||
MessageBox.Show("Введите Артикул", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
tbArticle.Focus();
|
||
return;
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(tbMinPrice.Text))
|
||
{
|
||
MessageBox.Show("Введите Минимальную цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
tbArticle.Focus();
|
||
return;
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(tbName.Text))
|
||
{
|
||
MessageBox.Show("Введите Название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
tbArticle.Focus();
|
||
return;
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(tbWidth.Text))
|
||
{
|
||
MessageBox.Show("Введите Ширину", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
tbArticle.Focus();
|
||
return;
|
||
}
|
||
|
||
if (cmbProduct.SelectedItem == null)
|
||
{
|
||
MessageBox.Show("Выберите тип продукта.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
cmbProduct.Focus();
|
||
return;
|
||
}
|
||
|
||
if (!decimal.TryParse(tbMinPrice.Text, out decimal price) || price < 0)
|
||
{
|
||
MessageBox.Show("Введите корректную цену (неотрицательное число)", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
tbMinPrice.Focus();
|
||
return;
|
||
}
|
||
|
||
if (!decimal.TryParse(tbWidth.Text, out decimal Width) || price < 0)
|
||
{
|
||
MessageBox.Show("Введите корректную ширину", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
tbWidth.Focus();
|
||
return;
|
||
}
|
||
|
||
if (!int.TryParse(tbArticle.Text, out int quantity) || quantity < 0)
|
||
{
|
||
MessageBox.Show("Введите корректный артикул", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
tbArticle.Focus();
|
||
return;
|
||
}
|
||
|
||
//ДОБАВЛЕНИЕ
|
||
var typeItem = (ComboItem)cmbProduct.SelectedItem;
|
||
string insert = @"insert into Products_import (idProduct_type_import, Name, Article, MinPrice, Width)
|
||
values (@idProduct_type_import, @Name, @Article, @MinPrice, @Width)";
|
||
var connec = DB.GetInstance().GetConnection();
|
||
if (connec.State == ConnectionState.Closed)
|
||
connec.Open();
|
||
using (var cmd = new MySqlCommand(insert, connec))
|
||
{
|
||
cmd.Parameters.AddWithValue("@idProduct_type_import", typeItem.Id);
|
||
cmd.Parameters.AddWithValue("@Name", tbName.Text.Trim());
|
||
cmd.Parameters.AddWithValue("@Article", tbArticle.Text.Trim());
|
||
cmd.Parameters.AddWithValue("@MinPrice", tbMinPrice.Text.Trim());
|
||
cmd.Parameters.AddWithValue("@Width", tbWidth.Text.Trim());
|
||
|
||
try
|
||
{
|
||
cmd.ExecuteNonQuery();
|
||
MessageBox.Show("Продукт успешно добавлен!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
this.DialogResult = DialogResult.OK;
|
||
this.Close();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show($"Ошибка при добавлении: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|
||
private void LoadTypes() //ЗАГРУЗКА ТИПОВ
|
||
{
|
||
string query = "select idProduct_type_import, TypeProduction from Product_type_import";
|
||
var conn = DB.GetInstance().GetConnection();
|
||
if(conn.State == ConnectionState.Closed)
|
||
{
|
||
conn.Open();
|
||
}
|
||
using (var cmd = new MySqlCommand(query, conn))
|
||
{
|
||
using (var reader = cmd.ExecuteReader())
|
||
{
|
||
while(reader.Read())
|
||
{
|
||
cmbProduct.Items.Add(new ComboItem
|
||
{
|
||
Id = reader.GetInt32("idProduct_type_import"),
|
||
Name = reader.GetString("TypeProduction")
|
||
});
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public class ComboItem //НУЖНО
|
||
{
|
||
public int Id { get; set; }
|
||
public string Name { get; set; }
|
||
|
||
public override string ToString()
|
||
{
|
||
return Name;
|
||
}
|
||
}
|
||
|
||
private void btnExit_Click(object sender, EventArgs e)
|
||
{
|
||
this.Hide();
|
||
}
|
||
}
|
||
}
|