CalculetionFormADD.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 CalculetionFormADD : Form
  14. {
  15. DataBase dataBase = new DataBase();
  16. public CalculetionFormADD()
  17. {
  18. InitializeComponent();
  19. // comboBox1 - организации
  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. }
  31. private void textBox1_TextChanged(object sender, EventArgs e)
  32. {
  33. }
  34. private void button_back_Click(object sender, EventArgs e)
  35. {
  36. this.Hide();
  37. CalculetionForm M = new CalculetionForm();
  38. M.ShowDialog();
  39. }
  40. private void Save_button_Click(object sender, EventArgs e)
  41. {
  42. var num = $"select Organizations_code from Organizations where Name = '{comboBox1.Text}'";
  43. SqlDataAdapter sda = new SqlDataAdapter(num, dataBase.GetConnection());
  44. DataTable dtbl = new DataTable();
  45. sda.Fill(dtbl);
  46. if (dtbl.Rows.Count == 0) MessageBox.Show("Поле организация должно быть выбрано из списка");
  47. else if (textBox1.Text.Replace(" ", "") == "") MessageBox.Show("Поле МГ1 не может быть пустым");
  48. else if (textBox2.Text.Replace(" ", "") == "") MessageBox.Show("Поле МГ2 не может быть пустым");
  49. else
  50. {
  51. dataBase.openConnection();
  52. SqlCommand sqlCommand_org = new SqlCommand(num, dataBase.GetConnection());
  53. var id_org = sqlCommand_org.ExecuteScalar().ToString();
  54. // Проверка есть ли у данной организации уже интернет
  55. string a = $"select count(*) from Calculation join Organizations on Calculation.Organizations_code = Organizations.Organizations_code where Organizations.Organizations_code='{id_org}' ";
  56. SqlCommand command1 = new SqlCommand(a, dataBase.GetConnection());
  57. if (command1.ExecuteScalar().ToString().Equals("1"))
  58. {
  59. MessageBox.Show("Данная организация уже есть в таблице");
  60. return;
  61. }
  62. var addQuery = $"insert into Calculation (Organizations_code, MG_OSIPS,MG_m200) values ('{id_org}','{textBox1.Text}','{textBox2.Text}')";
  63. var command = new SqlCommand(addQuery, dataBase.GetConnection());
  64. command.ExecuteNonQuery();
  65. MessageBox.Show("Запись успешно добавлена в таблицу");
  66. dataBase.closeConnection();
  67. }
  68. }
  69. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  70. {
  71. char number = e.KeyChar;
  72. if ((e.KeyChar <= 47 || e.KeyChar >= 58) && number != 8 && number != 46) //цифры, клавиша BackSpace и запятая а ASCII
  73. {
  74. e.Handled = true;
  75. }
  76. }
  77. private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
  78. {
  79. char number = e.KeyChar;
  80. if ((e.KeyChar <= 47 || e.KeyChar >= 58) && number != 8 && number != 46) //цифры, клавиша BackSpace и запятая а ASCII
  81. {
  82. e.Handled = true;
  83. }
  84. }
  85. private void global_FormClosed(object sender, FormClosedEventArgs e)
  86. {
  87. Application.Exit();
  88. }
  89. }
  90. }