FormEditingGuard.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 ImpulseVision
  12. {
  13. public partial class FormEditingGuard : Form
  14. {
  15. public FormEditingGuard(bool operations,string userID)
  16. {
  17. InitializeComponent();
  18. Operations = operations;
  19. UserID = userID;
  20. }
  21. #region <Переменные>
  22. //true - добавление; false - редактирование.
  23. private bool Operations { get; set; }
  24. private string UserID { get; set; }
  25. SqlConnection SCon = new SqlConnection(Properties.Settings.Default.ImpulseVisionAppConnectionString);
  26. #endregion
  27. private void FormEditingGuard_Load(object sender, EventArgs e)
  28. {
  29. // TODO: This line of code loads data into the 'impulseVisionAppDataSet1.Staffs' table. You can move, or remove it, as needed.
  30. this.staffsTableAdapter.Fill(this.impulseVisionAppDataSet1.Staffs);
  31. if (Operations)
  32. {
  33. BsGuard.AddNew();
  34. BtnSaveEditing.Text = "Добавить";
  35. }
  36. else
  37. {
  38. BsGuard.Filter = $@"ID = '{UserID}'";
  39. }
  40. LblUserType.Hide();
  41. }
  42. private void BtnSaveEditing_Click(object sender, EventArgs e)
  43. {
  44. bool IsNotCorrectData = TbxLastname.Text.Trim() == "" || TbxFirstname.Text.Trim() == "" || TbxPassportSeria.Text.Trim() == "" || TbxPassportNumber.Text.Trim() == "" || TbxLogin.Text.Trim() == "" || TbxPass.Text.Trim() == "";
  45. if(IsNotCorrectData)
  46. {
  47. MessageBox.Show("Заполните все поля и повторите попытку!", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  48. return;
  49. }
  50. if (TbxPassportSeria.Text.Trim().Length < 4)
  51. {
  52. MessageBox.Show("Введите серию паспорта и повторите попытку!","ImpulseVision",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
  53. return;
  54. }
  55. if (TbxPassportNumber.Text.Trim().Length < 6)
  56. {
  57. MessageBox.Show("Введите номер паспорта и повторите попытку!", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  58. return;
  59. }
  60. if (ExistsLogin(TbxLogin.Text.Trim()) && Operations)
  61. {
  62. MessageBox.Show("Пользователь с таким логином уже существует!\r\nПридумайте новый логин и повторите попытку. ", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  63. return;
  64. }
  65. if(TbxPass.Text.Trim().Length < 4)
  66. {
  67. MessageBox.Show("Длина пароля должна быть не менее 4-х символов!", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  68. return;
  69. }
  70. if (!Operations)
  71. {
  72. if (TbxConfirmPass.Text.Trim() != string.Empty)
  73. {
  74. if (TbxPass.Text.Trim() != TbxConfirmPass.Text.Trim())
  75. {
  76. MessageBox.Show("Подтвердите новый пароль!", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  77. return;
  78. }
  79. }
  80. }
  81. else
  82. {
  83. if (TbxConfirmPass.Text.Trim() != TbxPass.Text.Trim())
  84. {
  85. MessageBox.Show("Подтвердите новый пароль!", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  86. return;
  87. }
  88. }
  89. LblUserType.Text = "2";
  90. BsGuard.EndEdit();
  91. this.staffsTableAdapter.Update(this.impulseVisionAppDataSet1.Staffs);
  92. this.DialogResult = DialogResult.OK;
  93. Close();
  94. }
  95. /// <summary>
  96. /// проверка логина на существование
  97. /// </summary>
  98. /// <param name="login">логин</param>
  99. /// <returns>логическое значение true или false</returns>
  100. private bool ExistsLogin(string login)
  101. {
  102. bool Result = false;
  103. SCon.Open();
  104. string QueryCheckLogin = $@"select *
  105. from Staffs
  106. where [Login] = '{login}'";
  107. SqlCommand Cmd = new SqlCommand(QueryCheckLogin, SCon);
  108. SqlDataReader Res = Cmd.ExecuteReader();
  109. if(Res.HasRows)
  110. {
  111. Result = true;
  112. }
  113. SCon.Close();
  114. return Result;
  115. }
  116. private void TbxPassportSeria_Validated(object sender, EventArgs e)
  117. {
  118. if (TbxPassportSeria.Text.Trim().Length > 4)
  119. {
  120. TbxPassportSeria.Text = TbxPassportSeria.Text.Trim().Substring(0, 4);
  121. }
  122. }
  123. private void TbxPassportNumber_Validated(object sender, EventArgs e)
  124. {
  125. if (TbxPassportNumber.Text.Trim().Length > 6)
  126. {
  127. TbxPassportNumber.Text = TbxPassportNumber.Text.Trim().Substring(0, 6);
  128. }
  129. }
  130. }
  131. }