private void buttonSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxName.Text)) { MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(textBoxPrice.Text)) { MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (serviceDrugs == null || serviceDrugs.Count == 0) { MessageBox.Show("Заполните ингредиенты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { List <ServiceDrugBindingModel> productComponentBM = new List <ServiceDrugBindingModel>(); for (int i = 0; i < serviceDrugs.Count; ++i) { productComponentBM.Add(new ServiceDrugBindingModel { Id = serviceDrugs[i].Id, ServiceId = serviceDrugs[i].ServiceId, DrugId = serviceDrugs[i].DrugId, Count = serviceDrugs[i].Count }); } if (id.HasValue) { service.UpdElement(new ServiceBindingModel { Id = id.Value, ServiceName = textBoxName.Text, Price = Convert.ToInt32(textBoxPrice.Text), ServiceDrugs = productComponentBM }); } else { service.AddElement(new ServiceBindingModel { ServiceName = textBoxName.Text, Price = Convert.ToInt32(textBoxPrice.Text), ServiceDrugs = productComponentBM }); } MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } }