CalculetionFormUPD.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 CalculetionFormUPD : Form // расчет изменение
  14. {
  15. string id_num;
  16. DataBase dataBase = new DataBase();
  17. public CalculetionFormUPD(string id_num, string MG1, string MG2)
  18. {
  19. InitializeComponent();
  20. // comboBox1 - организации
  21. string searchString = $"Select Name From Organizations";
  22. SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection());
  23. dataBase.openConnection();
  24. SqlDataReader read = com.ExecuteReader();
  25. while (read.Read())
  26. {
  27. comboBox1.Items.Add(read.GetString(0));
  28. }
  29. dataBase.openConnection();
  30. this.id_num = id_num;
  31. comboBox1.Text = MG1;
  32. textBox2.Text = MG2;
  33. dataBase.closeConnection();
  34. }
  35. private void button_back_Click(object sender, EventArgs e)
  36. {
  37. this.Hide();
  38. CalculetionForm M = new CalculetionForm();
  39. M.ShowDialog();
  40. }
  41. private void Save_button_Click(object sender, EventArgs e) // сохраненрие
  42. {
  43. var num = $"select Organizations_code from Organizations where Name = '{comboBox1.Text}'";
  44. SqlDataAdapter sda = new SqlDataAdapter(num, dataBase.GetConnection());
  45. DataTable dtbl = new DataTable();
  46. sda.Fill(dtbl);
  47. if (dtbl.Rows.Count == 0) MessageBox.Show("Поле организация должно быть выбрано из списка");
  48. else if (textBox1.Text.Replace(" ", "") == "") MessageBox.Show("Поле МГ1 не может быть пустым");
  49. else if (textBox2.Text.Replace(" ", "") == "") MessageBox.Show("Поле МГ2 не может быть пустым");
  50. else
  51. {
  52. dataBase.openConnection();
  53. SqlCommand sqlCommand_org = new SqlCommand(num, dataBase.GetConnection());
  54. var id_org = sqlCommand_org.ExecuteScalar().ToString();
  55. var addQuery = $"update Calculation set Organizations_code= '{id_org}', MG_OSIPS = '{textBox1.Text}' , MG_m200 = '{textBox2.Text}' where Calculation_code = '{id_num}'";
  56. var command = new SqlCommand(addQuery, dataBase.GetConnection());
  57. command.ExecuteNonQuery();
  58. MessageBox.Show("Запись успешно изменена в таблице");
  59. dataBase.closeConnection();
  60. this.Hide();
  61. CalculetionForm n = new CalculetionForm();
  62. n.ShowDialog();
  63. }
  64. }
  65. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  66. {
  67. char number = e.KeyChar;
  68. if ((e.KeyChar <= 47 || e.KeyChar >= 58) && number != 8 && number != 46) //цифры, клавиша BackSpace и запятая а ASCII
  69. {
  70. e.Handled = true;
  71. }
  72. }
  73. private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
  74. {
  75. char number = e.KeyChar;
  76. if ((e.KeyChar <= 47 || e.KeyChar >= 58) && number != 8 && number != 46) //цифры, клавиша BackSpace и запятая а ASCII
  77. {
  78. e.Handled = true;
  79. }
  80. }
  81. private void global_FormClosed(object sender, FormClosedEventArgs e)
  82. {
  83. Application.Exit();
  84. }
  85. }
  86. }