NumberADD.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11. namespace NN_TelekomPP.Forms
  12. {
  13. public partial class NumberADD : Form // телефон добавить
  14. {
  15. DataBase dataBase = new DataBase();
  16. public NumberADD()
  17. {
  18. InitializeComponent();
  19. }
  20. private void NumberADD_Load(object sender, EventArgs e)
  21. {
  22. string searchString = $"Select Name From Organizations";
  23. SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection());
  24. dataBase.openConnection();
  25. SqlDataReader read = com.ExecuteReader();
  26. while (read.Read())
  27. {
  28. comboBox1.Items.Add(read.GetString(0));
  29. }
  30. read.Close();
  31. dataBase.closeConnection();
  32. }
  33. private void Save_button_Click(object sender, EventArgs e) // сохранение
  34. {
  35. var num = $"select Organizations_code from Organizations where Name = '{comboBox1.Text}'";
  36. SqlDataAdapter sda = new SqlDataAdapter(num, dataBase.GetConnection());
  37. DataTable dtbl = new DataTable();
  38. sda.Fill(dtbl);
  39. if (dtbl.Rows.Count == 0) MessageBox.Show("Поле организация должно быть выбрано из списка");
  40. else if (textBox2.Text.Replace(" ", "") == "") MessageBox.Show("Поле номер не может быть пустым");
  41. else
  42. {
  43. dataBase.openConnection();
  44. SqlCommand sqlCommand_org = new SqlCommand(num, dataBase.GetConnection());
  45. var id_org = sqlCommand_org.ExecuteScalar().ToString();
  46. var addQuery = $"insert into Number (Organizations_code, Number) values ('{id_org}','{textBox2.Text}')";
  47. var command = new SqlCommand(addQuery, dataBase.GetConnection());
  48. command.ExecuteNonQuery();
  49. MessageBox.Show("Запись успешно добавлена в таблицу");
  50. dataBase.closeConnection();
  51. }
  52. }
  53. private void button_back_Click(object sender, EventArgs e) // назад
  54. {
  55. this.Hide();
  56. Number M = new Number();
  57. M.ShowDialog();
  58. }
  59. private void global_FormClosed(object sender, FormClosedEventArgs e)
  60. {
  61. Application.Exit();
  62. }
  63. }
  64. }