2009-02-20 40 views
-1

有人請幫助我如何使用ArrayList動態存儲值。每次我想添加患者詳細信息。這裏是我的代碼層明智的:使用ArrayList需要幫助來存儲動態值

PatientDataLayer

public class PatientData 
{ 
    public string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString(); 
    public int AddPatient(Patient obj) 
    { 
     using (var con = new SqlConnection(str)) 
     { 
      using (var com = new SqlCommand("AddPatient", con)) 
      { 
       com.CommandType = CommandType.StoredProcedure; 
       com.Parameters.AddWithValue("@Name", obj.Name); 
       com.Parameters.AddWithValue("@Address", obj.Address); 
       com.Parameters.AddWithValue("@DateOfBirth", obj.DateOfBirth); 
       com.Parameters.AddWithValue("@Phone", obj.Phone); 
       com.Parameters.AddWithValue("@EmergencyContact", obj.EmergencyContact); 
       com.Parameters.AddWithValue("@DateOfRegistration", obj.DateOfRegistration); 
       con.Open(); 
       com.ExecuteNonQuery(); 
       con.Close(); 
       return 0;      
      } 
     } 
    } 

PatientBusinessLayer

public class PatientBusiness 
{ 
    public void Add(Patient obj) 
    {  
     PatientData pd = new PatientData(); 
     pd.AddPatient(obj);  
    } 
} 

Patient.aspx.cs

protected void BtnAdd_Click(object sender, EventArgs e) 
    { 
     if (!Page.IsValid) //validating the page 
      return; 
     string name = TxtName.Text; 
     string address = TxtAddress.Text; 
     DateTime dateofbirth =Convert.ToDateTime(TxtDateOfBirth.Text); 
     int phone = Convert.ToInt32(TxtPhone.Text); 
     int emergencyno=Convert.ToInt32(TxtContact.Text); 
     DateTime registrationdate =Convert.ToDateTime(TxtRegistrationDate.Text); 
     PatientBusiness PB = new PatientBusiness(); 
     Patient obj = new Patient(); 
     try 
     { 
      obj.Name = name; 
      obj.Address = address; 
      obj.DateOfBirth = dateofbirth; 
      obj.Phone = phone; 
      obj.EmergencyContact = emergencyno; 
      obj.DateOfRegistration = registrationdate; 
      PB.Add(obj); 
      LblMessage.Text = "Patient has been added successfully"; 
      TxtName.Text = ""; 
      TxtAddress.Text = ""; 
      TxtDateOfBirth.Text = ""; 
      TxtPhone.Text = ""; 
      TxtContact.Text = ""; 
      TxtRegistrationDate.Text = ""; 
     } 
     catch (Exception ee) 
     { 
      LblMessage.Text = ee.Message.ToString(); 
     } 
     finally 
     { 
      PB = null; 
     } 
    } 

感謝, Masum

+0

您應該格式化該代碼。 – Frank 2009-02-20 04:47:02

+1

你想在這裏用ArrayList做什麼?您正在保存單個記錄傳遞鍵入的對象...哪裏需要列表? – 2009-02-20 05:33:19

回答

1

我不明白你的問題,但在審查你的代碼後,我只能推薦你考慮使用與FormView結合的ObjectDataSource,並停止在代碼後面執行「業務內容」。