Services_customersUPD.cs 4.9 KB

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