2012-01-27 9 views
0

私は、ユーザーが患者データを入力し、保険ハブに渡される小さなWebフォームを持っています。私はちょうどいくつかの基本的な妥当性検査を提供したいと思っています。たとえば、社会保障番号の数値、10の年齢、就業年齢、特定の年齢番号などです。簡単な基本データ検証を行う方法

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
... 


namespace PBM 
{ 
    public partial class MainPage : UserControl 
    { 
     private DomainServicePBM _context = new DomainServicePBM(); 
     public MainPage() 
     { 
      InitializeComponent(); 
      this.ObjPatient = new Patient(); 
     } 

     private void btnSearch_Click(object sender, RoutedEventArgs e) 
     { 
      dgPatient.ItemsSource = _context.Patients; 
      _context.Load(_context.GetPatientsBySearchQuery(sTxtFirstName.Text.Trim(), sTxtLastName.Text.Trim(), sCombGender.SelectedIndex == 1 ? false: sCombGender.SelectedIndex == 2 ? true : new bool?())); 

      PagedCollectionView itemListView = new PagedCollectionView(dgPatient.ItemsSource); 
      dpPatient.Source = itemListView; 
     } 

     private void btnSave_Click(object sender, RoutedEventArgs e) 
     { 
      //_context = new DomainServicePBM(); 
      if (ObjPatient != null && ObjPatient.PatientID > 0) 
      { 
       Patient p = _context.Patients.Single(pat => pat.PatientID == this.ObjPatient.PatientID); 
       p.FirstName = txtFirstName.Text.Trim(); 
       p.LastName = txtLastName.Text.Trim(); 
       p.MiddleName = txtMiddleName.Text.Trim(); 
       p.Gender = cmbGender.SelectedIndex == 0 ? false : true; 
       p.DOB = ctrlDTDOB.SelectedDate; 
       p.Age = Convert.ToInt32(txtAge.Text.Trim()); 
       p.MaterialStatus = cmbMaritalStatus.SelectedIndex == 0 ? (byte)MaritalStatus.Single : 
        cmbMaritalStatus.SelectedIndex == 1 ? (byte)MaritalStatus.Married : (byte)MaritalStatus.Divorced; 
       p.SSN = txtSSN.Text.Trim(); 
       p.MedicalID = txtMedicalID.Text.Trim(); 
       p.Medicare = txtMedicare.Text.Trim(); 
       p.Race = txtRace.Text.Trim(); 
       p.AdmitFrom = ctrlDTAdmitFrom.SelectedDate; 
      } 
      else 
      { 
       _context.Patients.Add(new Patient 
       { 
        FirstName = txtFirstName.Text.Trim(), 
        LastName = txtLastName.Text.Trim(), 
        MiddleName = txtMiddleName.Text.Trim(), 
        Gender = cmbGender.SelectedIndex == 0 ? false : true, 
        DOB = ctrlDTDOB.SelectedDate, 
        Age = Convert.ToInt32(txtAge.Text.Trim()), 
        MaterialStatus = cmbMaritalStatus.SelectedIndex == 0 ? (byte)MaritalStatus.Single : 
        cmbMaritalStatus.SelectedIndex == 1 ? (byte)MaritalStatus.Married : (byte)MaritalStatus.Divorced, 
        SSN = txtSSN.Text.Trim(), 
        MedicalID = txtMedicalID.Text.Trim(), 
        Medicare = txtMedicare.Text.Trim(), 
        Race = txtRace.Text.Trim(), 
        AdmitFrom = ctrlDTAdmitFrom.SelectedDate 
       }); 
      } 
      _context.SubmitChanges(
       (SubmitOperation so) => 
       { 
        if (so.HasError) 
        { 
         MessageBox.Show("Patient Info Not Saved."); 
        } 
        else 
        { 
         MessageBox.Show("Patient Info Saved Successfully."); 
         ResetControls(); 
         this.ObjPatient = new Patient(); 
        } 
       }, null); 


     } 



     Patient ObjPatient { get; set; } 

     private void btnAdd_Click(object sender, RoutedEventArgs e) 
     { 
      this.ObjPatient = new Patient(); 
     } 

     private void ResetControls() 
     { 
      txtFirstName.Text = txtLastName.Text = txtMiddleName.Text = txtMedicalID.Text = txtMedicare.Text = txtRace.Text = txtSSN.Text = txtAge.Text = string.Empty; 
      cmbGender.SelectedIndex = cmbMaritalStatus.SelectedIndex = 0; 
      ctrlDTAdmitFrom.SelectedDate = ctrlDTDOB.SelectedDate = DateTime.Now; 
     } 

     private void btnDelete_Click(object sender, RoutedEventArgs e) 
     { 
      this.ObjPatient = dgPatient.SelectedItem as Patient; 
      if (this.ObjPatient != null) 
      { 
       _context.Patients.Remove(this.ObjPatient); 
       _context.SubmitChanges(
        (SubmitOperation so) => 
        { 
         if (so.HasError) 
         { 
          MessageBox.Show(so.Error.ToString()); 
         } 
         else 
         { 
          MessageBox.Show("Patient Deleted Successfully."); 
         } 

        }, null); 
      } 
      else 
      { 
       MessageBox.Show("Please select patient first."); 
      } 
     } 

     private void btnEdit_Click(object sender, RoutedEventArgs e) 
     { 
      this.ObjPatient = dgPatient.SelectedItem as Patient; 
      if (ObjPatient != null) 
      { 
       txtFirstName.Text = ObjPatient.FirstName; 
       txtLastName.Text = ObjPatient.LastName; 
       txtMiddleName.Text = ObjPatient.MiddleName; 
       cmbGender.SelectedIndex = ObjPatient.Gender == true ? 0 : 1; 
       cmbMaritalStatus.SelectedIndex = ObjPatient.MaterialStatus == 1 ? 0 : ObjPatient.MaterialStatus == 2 ? 1 : 2; 
       txtAge.Text = Convert.ToString(ObjPatient.Age); 
       ctrlDTDOB.SelectedDate = ObjPatient.DOB; 
       ctrlDTAdmitFrom.SelectedDate = Convert.ToDateTime(ObjPatient.AdmitFrom); 
       txtMedicalID.Text = ObjPatient.MedicalID; 
       txtMedicare.Text = ObjPatient.Medicare; 
       txtRace.Text = ObjPatient.Race; 
       txtSSN.Text = ObjPatient.SSN; 
      } 
     } 

     private void dpPatient_PageIndexChanged(object sender, EventArgs e) 
     { 
      _context.Patients.Skip(dpPatient.PageIndex).Take(1); 
     } 

    } 

    public enum MaritalStatus { 
     Single=0, 
     Married=1, 
     Divorced = 2 
    } 
} 

答えて

0

私はDataAnnations名前空間を見ていきます。あらゆる種類のバリデーションを定義するための属性を提供します。これらの属性は、検証が必要なプロパティに適用する必要があります。次に、これらの属性が検証をパスするかどうかを確認する方法が必要です。これは通常、反射を使用して行われます。このanswerをご覧ください。

関連する問題