Jes Sie

Jes Sie

  • 669
  • 1.2k
  • 284.7k

Inserting Header and Detail Values using c#

Mar 2 2018 2:58 AM
Hi all,
 
I've been developing a system for only 2 years. In other words, I'm still learning until now. One of the applications that I developed involves inserting header and details. My way of doing it is not the best one but I was able to do the task. However, there are times that the header fails to insert to DB but the details succeeded and vice versa. Below are my code snippet and I wish someone can share their ideas on how to make it in such a way that if the header fails, then the details value should not insert to  DB and vice versa.
 
  1. try  
  2.            {  
  3.                //for enrollment header  
  4.                header.SchoolYear = ddlSchoolYear.SelectedItem.Text;  
  5.                header.EnrollmentID = lblEnrollmentID.Text;  
  6.                header.StudentID = lblStudentID.Text;  
  7.                header.GradeLvl = ddlGradeLvl.SelectedItem.Text;  
  8.                header.EnrollmentFees = Convert.ToDouble(lblTotalFees.Text);  
  9.                header.Discount = Convert.ToDouble(txtDiscount.Text);  
  10.                header.PaymentTerms = Convert.ToInt32(ddlTermPayment.SelectedValue);  
  11.                header.SchoolCode = lblSchoolCode.Text;  
  12.                header.UserLogs = lblUsername.Text;  
  13.               
  14.                insert.InsertEnrollmentHeader(header);  
  15.   
  16.                //for enrollment charges  
  17.                #region ----charges  
  18.                foreach (GridViewRow row in gvCharges.Rows)  
  19.                {  
  20.                    if (row.RowType == DataControlRowType.DataRow)  
  21.                    {  
  22.                        CheckBox Charges = (CheckBox)row.FindControl("cbCharges");  
  23.                        if (Charges != null && Charges.Checked)  
  24.                        {  
  25.                            Label lblChargesID = (Label)row.FindControl("lblChargesID");  
  26.   
  27.                            charges.ChargesID = Convert.ToInt32(lblChargesID.Text);  
  28.                            charges.EnrollmentID = lblEnrollmentID.Text;  
  29.   
  30.                            count = insert.InsertEnrollmentCharges(charges);  
  31.                        }  
  32.                    }  
  33.                }  
 Thanks in advance.

Answers (2)