Services_customersADD.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 Services_customersADD : Form
  14. {
  15. DataBase dataBase = new DataBase();
  16. public Services_customersADD()
  17. {
  18. InitializeComponent();
  19. }
  20. private void Services_customersADD_Load(object sender, EventArgs e)
  21. {
  22. // comboBox1 - организации
  23. string searchString = $"Select Name From Organizations";
  24. SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection());
  25. dataBase.openConnection();
  26. SqlDataReader read = com.ExecuteReader();
  27. while (read.Read())
  28. {
  29. comboBox1.Items.Add(read.GetString(0));
  30. }
  31. read.Close();
  32. dataBase.closeConnection();
  33. // comboBox2 - Интернет
  34. string searchString2 = $"Select Name From Internet";
  35. SqlCommand com2 = new SqlCommand(searchString2, dataBase.GetConnection());
  36. dataBase.openConnection();
  37. SqlDataReader read2 = com2.ExecuteReader();
  38. while (read2.Read())
  39. {
  40. comboBox2.Items.Add(read2.GetString(0));
  41. }
  42. read2.Close();
  43. dataBase.closeConnection();
  44. // comboBox3 - Абонентская плата
  45. string searchString3 = $"Select Name From SubscriptionFee";
  46. SqlCommand com3 = new SqlCommand(searchString3, dataBase.GetConnection());
  47. dataBase.openConnection();
  48. SqlDataReader read3 = com3.ExecuteReader();
  49. while (read3.Read())
  50. {
  51. comboBox3.Items.Add(read3.GetString(0));
  52. }
  53. read3.Close();
  54. dataBase.closeConnection();
  55. }
  56. private void button_back_Click(object sender, EventArgs e)
  57. {
  58. this.Hide();
  59. Services_customers M = new Services_customers();
  60. M.ShowDialog();
  61. }
  62. private void Save_button_Click(object sender, EventArgs e)
  63. {
  64. var num = $"select Organizations_code from Organizations where Name = '{comboBox1.Text}'";
  65. var internet = $"select Internet_code from Internet where Name = '{comboBox2.Text}'";
  66. var Fee = $"select AP_code from SubscriptionFee where Name = '{comboBox3.Text}'";
  67. SqlDataAdapter sda = new SqlDataAdapter(num, dataBase.GetConnection());
  68. SqlDataAdapter sda2 = new SqlDataAdapter(internet, dataBase.GetConnection());
  69. SqlDataAdapter sda3 = new SqlDataAdapter(Fee, dataBase.GetConnection());
  70. DataTable dtbl = new DataTable();
  71. sda.Fill(dtbl);
  72. if (comboBox1.Text.Replace(" ", "") == "") MessageBox.Show("Поле организация не может быть пустым");
  73. else if (comboBox2.Text.Replace(" ", "") == "") MessageBox.Show("Поле интернет не может быть пустым");
  74. else if (comboBox3.Text.Replace(" ", "") == "") MessageBox.Show("Поле абонентский пакет не может быть пустым");
  75. else
  76. {
  77. dataBase.openConnection();
  78. SqlCommand sqlCommand_org = new SqlCommand(num, dataBase.GetConnection());
  79. SqlCommand sqlCommand_org2 = new SqlCommand(internet, dataBase.GetConnection());
  80. SqlCommand sqlCommand_org3 = new SqlCommand(Fee, dataBase.GetConnection());
  81. var id_org = sqlCommand_org.ExecuteScalar().ToString();
  82. var inter = sqlCommand_org2.ExecuteScalar().ToString();
  83. var AP1 = sqlCommand_org3.ExecuteScalar().ToString();
  84. // Проверка есть ли у данной организации уже интернет
  85. string a = $"select count(*) from Services_customers join Organizations on Services_customers.Organizations_code = Organizations.Organizations_code where Organizations.Organizations_code='{id_org}' ";
  86. SqlCommand command1 = new SqlCommand(a,dataBase.GetConnection());
  87. if (command1.ExecuteScalar().ToString().Equals("1"))
  88. {
  89. MessageBox.Show("Данная организация уже есть в таблице");
  90. return;
  91. }
  92. var addQuery = $"insert into Services_customers (Organizations_code, Internet_code, AP_code ) values ('{id_org}','{inter}', '{AP1}' )";
  93. var command = new SqlCommand(addQuery, dataBase.GetConnection());
  94. command.ExecuteNonQuery();
  95. MessageBox.Show("Запись успешно добавлена в таблицу");
  96. dataBase.closeConnection();
  97. }
  98. }
  99. private void global_FormClosed(object sender, FormClosedEventArgs e)
  100. {
  101. Application.Exit();
  102. }
  103. }
  104. }