コード例 #1
0
        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;
            }
        }
コード例 #2
0
        private async void AddPatientBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                AddPatientBtn.IsEnabled = false;
                string   firstName       = FirstNameTextBox.Text;
                string   lastName        = LastNameTextBox.Text;
                string   middleName      = MiddleNameTextBox.Text;
                string   address         = AddressTextBox.Text;
                string   insurancePolicy = InsurancePolicyTextBox.Text;
                DateTime dateOfBirth;
                if (string.IsNullOrWhiteSpace(firstName))
                {
                    throw new ArgumentNullException($"'{nameof(firstName)}' is incorrect");
                }
                if (string.IsNullOrWhiteSpace(lastName))
                {
                    throw new ArgumentNullException($"'{nameof(lastName)}' is incorrect");
                }
                if (string.IsNullOrWhiteSpace(middleName))
                {
                    throw new ArgumentNullException($"'{nameof(middleName)}' is incorrect");
                }
                if (string.IsNullOrWhiteSpace(address))
                {
                    throw new ArgumentNullException($"'{nameof(address)}' is incorrect");
                }
                if (string.IsNullOrWhiteSpace(insurancePolicy))
                {
                    throw new ArgumentNullException($"'{nameof(insurancePolicy)}' is incorrect");
                }
                if (!DateOfBirthDataPicker.SelectedDate.HasValue || DateOfBirthDataPicker.SelectedDate.Value > DateTime.Now)
                {
                    throw new ArgumentNullException($"'{nameof(dateOfBirth)}' is incorrect");
                }

                dateOfBirth = DateOfBirthDataPicker.SelectedDate.Value;

                Patient requestPatient = new Patient
                {
                    FirstName       = firstName,
                    LastName        = lastName,
                    MiddleName      = middleName,
                    Address         = address,
                    InsurancePolicy = insurancePolicy,
                    DateOfBirth     = dateOfBirth
                };

                string request = tcpService.SerializeAddPatientRequest(requestPatient, SingletoneObj.User);
                byte[] data    = await tcpService.CodeStreamAsync(request);

                await SingletoneObj.Stream.WriteAsync(data, 0, data.Length);

                string response = await tcpService.DecodeStreamAsync(SingletoneObj.Stream);

                var responseArgs = response.Split(';');
                if (responseArgs.Length > 1 && responseArgs[0].Contains("500"))
                {
                    throw new ArgumentException(responseArgs[1]);
                }
                this.Close();
            }
            catch (Exception ex)
            {
                AddPatientBtn.IsEnabled = true;
                StatusLabel.Content     = "Status: " + ex.Message;
            }
        }