2012-01-27 50 views
0

我有一個小型Web表單,用戶將輸入一些患者數據,並將其傳遞給保險中心。我只是想提供一些基本的驗證,例如社會安全號碼sb數字,十分之一的年齡,商業年齡和某個年齡的數字等等。我怎麼能這樣做,在這裏代碼?如何進行簡單的基本數據驗證

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

相關問題