NumberUPD.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 NumberUPD : Form // телефоны изменить
  14. {
  15. string id_num1;
  16. string name;
  17. public NumberUPD(string id_num, string name,string number)
  18. {
  19. InitializeComponent();
  20. string searchString = $"Select Name From Organizations";
  21. SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection());
  22. dataBase.openConnection();
  23. SqlDataReader read = com.ExecuteReader();
  24. while (read.Read())
  25. {
  26. comboBox1.Items.Add(read.GetString(0));
  27. }
  28. read.Close();
  29. dataBase.closeConnection();
  30. dataBase.openConnection();
  31. this.id_num1 = id_num;
  32. comboBox1.Text = name;
  33. textBox2.Text = number;
  34. dataBase.closeConnection();
  35. }
  36. DataBase dataBase = new DataBase();
  37. private void button_back_Click(object sender, EventArgs e) // назад
  38. {
  39. this.Hide();
  40. Number M = new Number();
  41. M.ShowDialog();
  42. }
  43. private void Save_button_Click(object sender, EventArgs e) // сохранение
  44. {
  45. var num = $"select Organizations_code from Organizations where Name = '{comboBox1.Text}'";
  46. SqlDataAdapter sda = new SqlDataAdapter(num, dataBase.GetConnection());
  47. DataTable dtbl = new DataTable();
  48. sda.Fill(dtbl);
  49. if (dtbl.Rows.Count == 0) MessageBox.Show("Поле организация должно быть выбрано из списка");
  50. else if (textBox2.Text.Replace(" ", "") == "") MessageBox.Show("Поле номер не может быть пустым");
  51. else
  52. {
  53. dataBase.openConnection();
  54. SqlCommand sqlCommand_org = new SqlCommand(num, dataBase.GetConnection());
  55. var id_org = sqlCommand_org.ExecuteScalar().ToString();
  56. var addQuery = $"update Number set Organizations_code = '{id_org}', Number = '{textBox2.Text}' where Number_code = '{id_num1}'";
  57. var command = new SqlCommand(addQuery, dataBase.GetConnection());
  58. command.ExecuteNonQuery();
  59. MessageBox.Show("Запись успешно изменена в таблице");
  60. dataBase.closeConnection();
  61. this.Hide();
  62. Number n = new Number();
  63. n.ShowDialog();
  64. }
  65. }
  66. private void global_FormClosed(object sender, FormClosedEventArgs e)
  67. {
  68. Application.Exit();
  69. }
  70. }
  71. }