161 lines
5.6 KiB
C#
161 lines
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using System.Windows.Forms.VisualStyles;
|
|
using MySql.Data.MySqlClient;
|
|
|
|
namespace demMalyhin
|
|
{
|
|
public partial class AdmAddOrder : Form
|
|
{
|
|
public AdmAddOrder()
|
|
{
|
|
InitializeComponent();
|
|
LoadPickup();
|
|
LoadUser();
|
|
}
|
|
|
|
private void LoadPickup()
|
|
{
|
|
try
|
|
{
|
|
DB db = new DB();
|
|
using (MySqlConnection connection = db.getConnection())
|
|
{
|
|
connection.Open();
|
|
string query = "SELECT idpickup_points, pickup_points.index FROM pickup_points";
|
|
using (MySqlCommand command = new MySqlCommand(query, connection))
|
|
{
|
|
using (MySqlDataReader reader = command.ExecuteReader())
|
|
{
|
|
DataTable categoryTable = new DataTable();
|
|
categoryTable.Load(reader);
|
|
|
|
comboBoxPoint.DataSource = categoryTable;
|
|
comboBoxPoint.DisplayMember = "index";
|
|
comboBoxPoint.ValueMember = "idpickup_points";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (MySqlException ex)
|
|
{
|
|
MessageBox.Show("Ошибка при загрузке категорий: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
private void LoadUser()
|
|
{
|
|
try
|
|
{
|
|
DB db = new DB();
|
|
using (MySqlConnection connection = db.getConnection())
|
|
{
|
|
connection.Open();
|
|
string query = "SELECT id_users FROM users";
|
|
using (MySqlCommand command = new MySqlCommand(query, connection))
|
|
{
|
|
using (MySqlDataReader reader = command.ExecuteReader())
|
|
{
|
|
DataTable userTable = new DataTable();
|
|
userTable.Load(reader);
|
|
|
|
comboBoxUser.DataSource = userTable;
|
|
comboBoxUser.DisplayMember = "id_users";
|
|
comboBoxUser.ValueMember = "id_users";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (MySqlException ex)
|
|
{
|
|
MessageBox.Show("Ошибка при загрузке категорий: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
private async void btnAdd_Click(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(textBoxDate.Text) || string.IsNullOrEmpty(textBoxDelivery.Text) || string.IsNullOrEmpty(textBoxCode.Text))
|
|
{
|
|
MessageBox.Show("Заполните все обязательные поля!");
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
DB db = new DB();
|
|
|
|
try
|
|
{
|
|
db.openConnection();
|
|
|
|
string query = "INSERT INTO Orders (order_date, delivery_date, pickup_point_id, user_initials_id, Orders.code, orderstatus_id)" +
|
|
"VALUES (@od, @dd, @pp, (SELECT id_users FROM users WHERE id_users = @user), @code, 1)";
|
|
|
|
using (MySqlCommand command = new MySqlCommand(query, db.getConnection()))
|
|
{
|
|
command.Parameters.AddWithValue("@od", textBoxDate.Text);
|
|
command.Parameters.AddWithValue("@pp", comboBoxPoint.SelectedValue.ToString());
|
|
command.Parameters.AddWithValue("@user", comboBoxUser.SelectedValue.ToString());
|
|
command.Parameters.AddWithValue("@dd", textBoxDelivery.Text);
|
|
command.Parameters.AddWithValue("@code", textBoxCode.Text);
|
|
|
|
|
|
|
|
int rowsAffected = await command.ExecuteNonQueryAsync();
|
|
|
|
if (rowsAffected > 0)
|
|
{
|
|
MessageBox.Show("Заказ успешно добавлен!");
|
|
DialogResult = DialogResult.OK;
|
|
this.Close();
|
|
AdmOrder ado = new AdmOrder();
|
|
ado.Show();
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Ошибка при добавлении заказа.");
|
|
}
|
|
}
|
|
}
|
|
catch (MySqlException ex)
|
|
{
|
|
if (ex.Number == 1452)
|
|
{
|
|
MessageBox.Show("Указанная категория не существует. Введите допустимое название категории.");
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Ошибка базы данных: " + ex.Message);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show("Произошла: " + ex.Message);
|
|
}
|
|
finally
|
|
{
|
|
db.closeConnection();
|
|
}
|
|
}
|
|
|
|
private void btnBack_Click(object sender, EventArgs e)
|
|
{
|
|
this.Hide();
|
|
AdmOrder ador = new AdmOrder();
|
|
ador.Show();
|
|
}
|
|
|
|
private void btnExit_Click(object sender, EventArgs e)
|
|
{
|
|
Application.Exit();
|
|
}
|
|
}
|
|
}
|