ADD_customersUPD.cs 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 ADD_customersUPD : Form // изменение ДВО для абонентов
  14. {
  15. public int razmer = 0;
  16. public static string[] upd_addcus = new string[1];
  17. string id_serv;
  18. DataBase dataBase = new DataBase();
  19. public ADD_customersUPD(string id_num, string name1, string name2)
  20. {
  21. InitializeComponent();
  22. dataBase.openConnection(); // для изменения
  23. this.id_serv = id_num;
  24. comboBox1.Text = name1;
  25. comboBox2.Text = name2;
  26. dataBase.closeConnection();
  27. // comboBox1 - организации
  28. string searchString = $"Select Name From Organizations";
  29. SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection());
  30. dataBase.openConnection();
  31. SqlDataReader read = com.ExecuteReader();
  32. while (read.Read())
  33. {
  34. comboBox1.Items.Add(read.GetString(0));
  35. }
  36. read.Close();
  37. dataBase.closeConnection();
  38. // comboBox4 - дополнительные услуги
  39. string searchString4 = $"Select Name From ServicesADD";
  40. SqlCommand com4 = new SqlCommand(searchString4, dataBase.GetConnection());
  41. dataBase.openConnection();
  42. SqlDataReader read4 = com4.ExecuteReader();
  43. while (read4.Read())
  44. {
  45. comboBox2.Items.Add(read4.GetString(0));
  46. }
  47. read4.Close();
  48. dataBase.closeConnection();
  49. }
  50. private void button_back_Click(object sender, EventArgs e)
  51. {
  52. this.Hide();
  53. ADD_customers M = new ADD_customers();
  54. M.ShowDialog();
  55. }
  56. private void Save_button_Click(object sender, EventArgs e) // сохранение
  57. {
  58. var num = $"select Organizations_code from Organizations where Name = '{comboBox1.Text}'";
  59. var internet = $"select ServicesADD_code from ServicesADD where Name = '{comboBox2.Text}'";
  60. SqlDataAdapter sda = new SqlDataAdapter(num, dataBase.GetConnection());
  61. SqlDataAdapter sda2 = new SqlDataAdapter(internet, dataBase.GetConnection());
  62. DataTable dtbl = new DataTable();
  63. DataTable dtbl2 = new DataTable();
  64. sda.Fill(dtbl);
  65. sda2.Fill(dtbl2);
  66. if (comboBox1.Text.Replace(" ", "") == "") MessageBox.Show("Поле название организациb не может быть пустым");
  67. else if (comboBox2.Text.Replace(" ", "") == "") MessageBox.Show("Поле дополнительная услуга не может быть пустым");
  68. else
  69. {
  70. dataBase.openConnection();
  71. SqlCommand sqlCommand_org = new SqlCommand(num, dataBase.GetConnection());
  72. SqlCommand sqlCommand_org2 = new SqlCommand(internet, dataBase.GetConnection());
  73. var id_org = sqlCommand_org.ExecuteScalar().ToString();
  74. var inter = sqlCommand_org2.ExecuteScalar().ToString();
  75. var addQuery = $"update ADD_customers set Organizations_code = '{id_org}', ServicesADD_code = '{inter}' where ADD_customers.ADD_code = '{id_serv}'";
  76. var command = new SqlCommand(addQuery, dataBase.GetConnection());
  77. command.ExecuteNonQuery();
  78. MessageBox.Show("Запись успешно изменена");
  79. upd_addcus[razmer] = comboBox2.Text;
  80. Array.Resize(ref upd_addcus, upd_addcus.Length + 1);
  81. razmer++;
  82. dataBase.closeConnection();
  83. this.Hide();
  84. ADD_customers IF = new ADD_customers();
  85. IF.ShowDialog();
  86. }
  87. }
  88. private void global_FormClosed(object sender, FormClosedEventArgs e)
  89. {
  90. Application.Exit();
  91. }
  92. }
  93. }