private async void LoginBtn_Click(object sender, EventArgs e) { try { int port = int.Parse(this.PortTextBox.Text); if (port < 0) { throw new ArgumentException("'port' is incorrect"); } if (string.IsNullOrWhiteSpace(this.IPTextBox.Text)) { throw new ArgumentException("'ip' is incorrect"); } if (string.IsNullOrWhiteSpace(this.LoginTextBox.Text)) { throw new ArgumentException("'login' is incorrect"); } if (string.IsNullOrWhiteSpace(this.PasswordTextBox.Text)) { throw new ArgumentException("'pasword' is incorrect"); } string address = this.IPTextBox.Text; string login = this.LoginTextBox.Text; string password = this.PasswordTextBox.Text; Program.client = new TcpClient(address, port); Program.stream = Program.client.GetStream(); string request = tcpService.SerializeAuthorizeRequest(login, password); byte[] data = tcpService.CodeStream(request); Program.stream.Write(data, 0, data.Length); string response = tcpService.DecodeStream(Program.stream); User user = tcpService.DeserializeAuthorizeResponse(response); if (user.Login == null || user.Password == null || !user.Login.Equals(login) || !user.Password.Equals(password)) { throw new ArgumentException("login or password is incorrect"); } Program.user = user; Program.ip = address; Program.port = port; Form mainForm = new MainForm(); mainForm.Left = this.Left; mainForm.Top = this.Top; mainForm.Show(); this.Hide(); } catch (Exception ex) { this.StatusLabel.Text = "Status: " + ex.Message; if (Program.client != null) { Program.client.Close(); } if (Program.stream != null) { Program.stream.Close(); } } }
private void AddBtn_Click(object sender, EventArgs e) { try { string firstName = this.FirstNameTextBox.Text; string middleName = this.MiddleNameTextBox.Text; string lastName = this.LastNameTextBox.Text; string address = this.AddressTextBox.Text; string insurancePolicy = this.InsurancePolicyTextBox.Text; DateTime dateOfBirth = this.DateOfBirthPicker.Value; //validation if (string.IsNullOrWhiteSpace(firstName)) { throw new ArgumentException("'firstName' is incorrect"); } if (string.IsNullOrWhiteSpace(middleName)) { throw new ArgumentException("'middleName' is incorrect"); } if (string.IsNullOrWhiteSpace(lastName)) { throw new ArgumentException("'lastName' is incorrect"); } if (string.IsNullOrWhiteSpace(address)) { throw new ArgumentException("'address' is incorrect"); } if (string.IsNullOrWhiteSpace(insurancePolicy)) { throw new ArgumentException("'insurancePolicy' is incorrect"); } if (dateOfBirth < new DateTime(1900, 1, 1) || dateOfBirth > DateTime.Now) { throw new ArgumentException("'dateOfBirth' is incorrect"); } var queryPatient = new Patient { FirstName = firstName, LastName = lastName, MiddleName = middleName, Address = address, InsurancePolicy = insurancePolicy, DateOfBirth = dateOfBirth }; string request = tcpService.SerializeAddPatientRequest(queryPatient); byte[] data = tcpService.CodeStream(request); Program.stream.Write(data, 0, data.Length); string response = tcpService.DecodeStream(Program.stream); string[] responseArgs = response.Split(';'); if (responseArgs.Length == 0 || responseArgs[0].Equals("500")) { if (responseArgs.Length == 2) { throw new Exception(responseArgs[1]); } throw new Exception("Error!"); } this.Close(); } catch (Exception ex) { this.StatusLabel.Text = "Status: " + ex.Message; } }