РЕДАКТИРОВАНИЕ
using System;
using System.Data;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace TEST
{
public partial class MainForm : Form
{
private int selectedRequestId = -1; // Для хранения ID выбранной записи
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
LoadMasters();
LoadClients();
LoadRequests();
}
private void LoadMasters()
{
try
{
DB db = new DB();
MySqlCommand command = new MySqlCommand("SELECT id, fio FROM masters", db.getConnection());
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
DataTable table = new DataTable();
adapter.Fill(table);
masterComboBox.DataSource = table;
masterComboBox.DisplayMember = "fio";
masterComboBox.ValueMember = "id";
}
catch (Exception ex)
{
MessageBox.Show("Ошибка загрузки данных мастеров: " + ex.Message);
}
}
private void LoadClients()
{
try
{
DB db = new DB();
MySqlCommand command = new MySqlCommand("SELECT id, fio FROM clients", db.getConnection());
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
DataTable table = new DataTable();
adapter.Fill(table);
clientComboBox.DataSource = table;
clientComboBox.DisplayMember = "fio";
clientComboBox.ValueMember = "id";
}
catch (Exception ex)
{
MessageBox.Show("Ошибка загрузки данных клиентов: " + ex.Message);
}
}
private void LoadRequests()
{
try
{
DB db = new DB();
MySqlCommand command = new MySqlCommand("SELECT * FROM requests", db.getConnection());
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
DataTable table = new DataTable();
adapter.Fill(table);
dataGridView1.DataSource = table;
}
catch (Exception ex)
{
MessageBox.Show("Ошибка загрузки данных заявок: " + ex.Message);
}
}
private void submitButton_Click(object sender, EventArgs e)
{
String carType = deviceTypeField.Text;
String carModel = deviceModelField.Text;
String problemDescription = problemDescriptionField.Text;
String startDate = DateTime.Now.ToString("yyyy-MM-dd");
String requestStatus = "Pending";
String completionDate = null;
int masterID = Convert.ToInt32(masterComboBox.SelectedValue);
int clientID = Convert.ToInt32(clientComboBox.SelectedValue);
if (string.IsNullOrEmpty(carType) string.IsNullOrEmpty(carModel)
string.IsNullOrEmpty(problemDescription))
{
MessageBox.Show("Заполните все поля");
return;
}
try
{
DB db = new DB();
MySqlCommand command = new MySqlCommand("INSERT INTO requests (carType, carModel, problemDescription, startDate, requestStatus, completionDate, masterID, clientID) VALUES (@carType, @carModel, @problemDescription, @startDate, @requestStatus, @completionDate, @masterID, @clientID);", db.getConnection());
command.Parameters.Add("@carType", MySqlDbType.VarChar).Value = carType;
command.Parameters.Add("@carModel", MySqlDbType.VarChar).Value = carModel;
command.Parameters.Add("@problemDescription", MySqlDbType.VarChar).Value = problemDescription;
command.Parameters.Add("@startDate", MySqlDbType.VarChar).Value = startDate;
command.Parameters.Add("@requestStatus", MySqlDbType.VarChar).Value = requestStatus;
command.Parameters.Add("@completionDate", MySqlDbType.VarChar).Value = (object)completionDate ?? DBNull.Value;
command.Parameters.Add("@masterID", MySqlDbType.Int32).Value = masterID;
command.Parameters.Add("@clientID", MySqlDbType.Int32).Value = clientID;
db.openConnnection();
if (command.ExecuteNonQuery() == 1)
{
MessageBox.Show("Заявка была успешно отправлена");
LoadRequests();
}
else
{
MessageBox.Show("Ошибка при отправке заявки");
}
db.closeConnnection();
}
catch (Exception ex)
{
MessageBox.Show("Ошибка отправки данных: " + ex.Message);
}
}
private void updateButton_Click(object sender, EventArgs e)
{
if (selectedRequestId == -1)
{
MessageBox.Show("Выберите заявку для редактирования");
return;
}
String carType = deviceTypeField.Text;
String carModel = deviceModelField.Text;
String problemDescription = problemDescriptionField.Text;
String requestStatus = "Pending"; // Используйте значение, соответствующее вашему приложению
int masterID = Convert.ToInt32(masterComboBox.SelectedValue);
int clientID = Convert.ToInt32(clientComboBox.SelectedValue);
if (string.IsNullOrEmpty(carType) string.IsNullOrEmpty(carModel)
string.IsNullOrEmpty(problemDescription))
{
MessageBox.Show("Заполните все поля");
return;
}
try
{
DB db = new DB();
MySqlCommand command = new MySqlCommand("UPDATE requests SET carType = @carType, carModel = @carModel, problemDescription = @problemDescription, requestStatus = @requestStatus, masterID = @masterID, clientID = @clientID WHERE requestID = @requestID", db.getConnection());
command.Parameters.Add("@carType", MySqlDbType.VarChar).Value = carType;
command.Parameters.Add("@carModel", MySqlDbType.VarChar).Value = carModel;
command.Parameters.Add("@problemDescription", MySqlDbType.VarChar).Value = problemDescription;
command.Parameters.Add("@requestStatus", MySqlDbType.VarChar).Value = requestStatus;
command.Parameters.Add("@masterID", MySqlDbType.Int32).Value = masterID;
command.Parameters.Add("@clientID", MySqlDbType.Int32).Value = clientID;
command.Parameters.Add("@requestID", MySqlDbType.Int32).Value = selectedRequestId;
db.openConnnection();
if (command.ExecuteNonQuery() == 1)
{
MessageBox.Show("Заявка была успешно обновлена");
LoadRequests();
}
else
{
MessageBox.Show("Ошибка при обновлении заявки");
}
db.closeConnnection();
}
catch (Exception ex)
{
MessageBox.Show("Ошибка обновления данных: " + ex.Message);
}
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
selectedRequestId = Convert.ToInt32(selectedRow.Cells["requestID"].Value);
deviceTypeField.Text = selectedRow.Cells["carType"].Value.ToString();
deviceModelField.Text = selectedRow.Cells["carModel"].Value.ToString();
problemDescriptionField.Text = selectedRow.Cells["problemDescription"].Value.ToString();
masterComboBox.SelectedValue = selectedRow.Cells["masterID"].Value;
clientComboBox.SelectedValue = selectedRow.Cells["clientID"].Value;
}
}
}
}