protected void Page_Load(object sender, EventArgs e)
    {
        dbData = new string[7] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
        weekEndingTB.Enabled = false;
        // Fields to populate when user visits this page: Employee ID, Employee Name, and Branch
        TimesheetManager tm = new TimesheetManager();
        projectNumTB.Enabled = true;
        if (!IsPostBack)
        {
            // populate generic non-chargeable drop down list elements
            specifyDDL.DataSource = tm.getWorkType();
            specifyDDL.DataBind();
            specifyDDL.Items.Insert(0, "Select");
            detailsDDL.DataSource = tm.getExpenses();
            detailsDDL.DataBind();
            detailsDDL.Items.Insert(0, "Select");

            // sub-menu's of non-chargeable section
            NonChargeExpensesPanel.Visible = false;
            NonChargeHoursPanel.Visible = false;

            // check query strings for form data to be populated
            if (Request.QueryString.Count > 0)
            {
                string timesheet = Request.QueryString["time"];
                string expense = Request.QueryString["expense"];
                string hours = Request.QueryString["hours"];
                string project = Request.QueryString["project"];
                string classification = Request.QueryString["class"];
                string activity = Request.QueryString["activity"];
                int chID = Convert.ToInt32(Request.QueryString["charge"]);
                int nonChID = Convert.ToInt32(Request.QueryString["nonCh"]);
                string delete = Request.QueryString["delete"];
                int lab = Convert.ToInt32(Request.QueryString["test"]);

                // reset weekending dates
                ScriptManager.RegisterStartupScript(this, this.GetType(), "populate", "populate();", true);

                int id = Convert.ToInt32(timesheet);
                timesheetID = id;
                HiddenField1.Value = id.ToString();

                // handle lab hours
                if (lab > 0 && delete != "delete")
                {
                    List<ManagerTest> tests = tm.getManagerTestsForTimeSheet(id);
                    updateLabTestsBtn.Visible = true;
                    labTestsBtn.Visible = false;
                    aID = 1;
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "toggleAccordion", "accordionToggle();", true);
                    foreach (ManagerTest item in tests)
                    {
                        if (item.Day == "Sunday")
                        {
                            sunLabTB.Text = item.LabTest.ToString();
                            sunNuclearDensityTestTB.Text = item.DensityTest.ToString();
                        }
                        else if (item.Day == "Monday")
                        {
                            monLabTB.Text = item.LabTest.ToString();
                            monNuclearDensityTestTB.Text = item.DensityTest.ToString();
                        }
                        else if (item.Day == "Tuesday")
                        {
                            tuesLabTB.Text = item.LabTest.ToString();
                            tuesNuclearDensityTestTB.Text = item.DensityTest.ToString();
                        }
                        else if (item.Day == "Wednesday")
                        {
                            wedsLabTB.Text = item.LabTest.ToString();
                            wedsNuclearDensityTestTB.Text = item.DensityTest.ToString();
                        }
                        else if (item.Day == "Thursday")
                        {
                            thursLabTB.Text = item.LabTest.ToString();
                            thursNuclearDensityTestTB.Text = item.DensityTest.ToString();
                        }
                        else if (item.Day == "Friday")
                        {
                            friLabTB.Text = item.LabTest.ToString();
                            friNuclearDensityTestTB.Text = item.DensityTest.ToString();
                        }
                        else if (item.Day == "Saturday")
                        {
                            satLabTB.Text = item.LabTest.ToString();
                            satNuclearDensityTestTB.Text = item.DensityTest.ToString();
                        }
                    }
                }

                // set static properties on the webpage
                weekEndingTB.Text = tm.getWeekendingForTimeSheet(id);
                // if there is a query string, load the totals from the corresponding timesheet

                if (delete == "delete")
                {

                   tm.deleteSummary(nonChID, chID, id, lab, true);
                    summaryGV.Focus();
                }
                // update Timesheet Totals
                updateTimeSheetTotals();
                TimeSheet t = tm.getTimesheetForID(timesheetID);
                // get and populate user information
                User emp = tm.getEmployeeForId(t.EmployeeId);
                empIdTB.Text = emp.empNo;
                nameTB.Text = emp.FirstName + " " + emp.MiddleName + " " + emp.LastName;
                departmentTB.Text = emp.branch;
                if (Request.QueryString["projectOnly"] != null)
                {
                    int projectID = Convert.ToInt32(project);
                    Project p = tm.getProjectForID(projectID);
                    projectNumTB.Text = p.ProjectNo;
                    projectNameTB.Text = p.ProjectName;
                    clientNameTB.Text = p.ClientName;
                    classificationDDL.DataSource = tm.getClassificationForProject(p.ProjectNo);
                    classificationDDL.DataBind();
                    classificationDDL.SelectedValue = classification;

                    projectNumTB.Enabled = true;
                    aID = 0;
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "toggleAccordion", "accordionToggle();", true);
                }
                // Chargeable
                else if (project != null && project != "0" && project != string.Empty)
                {
                    // adjust accordion panels
                    aID = 0;
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "toggleAccordion", "accordionToggle();", true);
                    // populate the project information fields
                    int projectID = Convert.ToInt32(project);
                    Project p = tm.getProjectForID(projectID);
                    if (p != null)
                    {
                        projectNumTB.Text = p.ProjectNo;
                        projectNameTB.Text = p.ProjectName;
                        clientNameTB.Text = p.ClientName;
                        classificationDDL.DataSource = tm.getClassificationForProject(p.ProjectNo);
                        classificationDDL.DataBind();
                        classificationDDL.SelectedValue = classification;

                        activitiesDDL.DataSource = tm.getActivityForClassification(classification);
                        activitiesDDL.DataBind();
                        activitiesDDL.SelectedValue = activity;

                        // adjust buttons
                        chargeableBtn.Visible = false;
                        updateChargeableBtn.Visible = true;

                        // populate the chargeable fields based on the classification and activity selected
                        List<ManagerChargeable> cJobs = tm.getManagerChargeableForID(timesheetID);

                        if (chID > 0)
                        {
                            cJobs.Add(tm.getManagerChargeForID(chID));
                        }

                        if (cJobs != null)
                        {
                            foreach (var item in cJobs)
                            {
                                if (item.Day == "Sunday" && item.Classification == classification && item.Activity == activity)
                                {
                                    // Accomodation

                                    if (item.BillingAccomodation.HasValue)
                                    {
                                        sunBAccomTB.Text = item.PayRollAccomodation.Value.ToString("0.00##");
                                    }

                                    // Hours
                                    if (item.BillingHours.HasValue)
                                    {
                                        sunBCHoursTB.Text = item.BillingHours.Value.ToString("0.0");
                                    }
                                    // Distance
                                    if (item.BillingTravelDistance.HasValue)
                                    {
                                        sunBDistTB.Text = item.BillingTravelDistance.ToString();
                                    }

                                    // truck distance
                                    if (item.TruckDistance.HasValue)
                                    {
                                        sunTruckDistTB.Text = item.TruckDistance.ToString();
                                    }

                                    // Misc

                                    if (item.BillingMisc.HasValue)
                                    {
                                        sunBMiscTB.Text = item.BillingMisc.Value.ToString("0.00##");
                                    }

                                    sunCRemarksTB.Text = item.Remarks;
                                }
                                else if (item.Day == "Monday" && item.Classification == classification && item.Activity == activity)
                                {
                                    // Accomodation

                                    if (item.BillingAccomodation.HasValue)
                                    {
                                        monBAccomTB.Text = item.PayRollAccomodation.Value.ToString("0.00##");
                                    }

                                    // Hours

                                    if (item.BillingHours.HasValue)
                                    {
                                        monBCHoursTB.Text = item.BillingHours.Value.ToString("0.0");
                                    }
                                    // Distance

                                    if (item.BillingTravelDistance.HasValue)
                                    {
                                        monBDistTB.Text = item.BillingTravelDistance.ToString();
                                    }
                                    // truck distance
                                    if (item.TruckDistance.HasValue)
                                    {
                                        monTruckDistTB.Text = item.TruckDistance.ToString();
                                    }
                                    // Misc

                                    if (item.BillingMisc.HasValue)
                                    {
                                        monBMiscTB.Text = item.BillingMisc.Value.ToString("0.00##");
                                    }
                                    monCRemarksTB.Text = item.Remarks;
                                }
                                else if (item.Day == "Tuesday" && item.Classification == classification && item.Activity == activity)
                                {
                                    // Accomodation

                                    if (item.BillingAccomodation.HasValue)
                                    {
                                        tuesBAccomTB.Text = item.PayRollAccomodation.Value.ToString("0.00##");
                                    }

                                    // Hours

                                    if (item.BillingHours.HasValue)
                                    {
                                        tuesBCHoursTB.Text = item.BillingHours.Value.ToString("0.0");
                                    }
                                    // Distance

                                    if (item.BillingTravelDistance.HasValue)
                                    {
                                        tuesBDistTB.Text = item.BillingTravelDistance.ToString();
                                    }
                                    // truck distance
                                    if (item.TruckDistance.HasValue)
                                    {
                                        tuesTruckDistTB.Text = item.TruckDistance.ToString();
                                    }
                                    // Misc

                                    if (item.BillingMisc.HasValue)
                                    {
                                        tuesBMiscTB.Text = item.BillingMisc.Value.ToString("0.00##");
                                    }
                                    tuesCRemarksTB.Text = item.Remarks;
                                }
                                else if (item.Day == "Wednesday" && item.Classification == classification && item.Activity == activity)
                                {
                                    // Accomodation

                                    if (item.BillingAccomodation.HasValue)
                                    {
                                        wedsBAccomTB.Text = item.PayRollAccomodation.Value.ToString("0.00##");
                                    }

                                    // Hours

                                    if (item.BillingHours.HasValue)
                                    {
                                        wedsBCHoursTB.Text = item.BillingHours.Value.ToString("0.0");
                                    }
                                    // Distance

                                    if (item.BillingTravelDistance.HasValue)
                                    {
                                        wedsBDistTB.Text = item.BillingTravelDistance.ToString();
                                    }
                                    // truck distance
                                    if (item.TruckDistance.HasValue)
                                    {
                                        wedsTruckDistTB.Text = item.TruckDistance.ToString();
                                    }
                                    // Misc

                                    if (item.BillingMisc.HasValue)
                                    {
                                        wedsBMiscTB.Text = item.BillingMisc.Value.ToString("0.00##");
                                    }
                                    wedsCRemarksTB.Text = item.Remarks;
                                }
                                else if (item.Day == "Thursday" && item.Classification == classification && item.Activity == activity)
                                {
                                    // Accomodation

                                    if (item.BillingAccomodation.HasValue)
                                    {
                                        thursBAccomTB.Text = item.PayRollAccomodation.Value.ToString("0.00##");
                                    }

                                    // Hours
                                    if (item.BillingHours.HasValue)
                                    {
                                        thursBCHoursTB.Text = item.BillingHours.Value.ToString("0.0");
                                    }
                                    // Distance

                                    if (item.BillingTravelDistance.HasValue)
                                    {
                                        thursBDistTB.Text = item.BillingTravelDistance.ToString();
                                    }
                                    // truck distance
                                    if (item.TruckDistance.HasValue)
                                    {
                                        thursTruckDistTB.Text = item.TruckDistance.ToString();
                                    }
                                    // Misc

                                    if (item.BillingMisc.HasValue)
                                    {
                                        thursBMiscTB.Text = item.BillingMisc.Value.ToString("0.00##");
                                    }
                                    thursCRemarksTB.Text = item.Remarks;
                                }
                                else if (item.Day == "Friday" && item.Classification == classification && item.Activity == activity)
                                {    // Accomodation
                                    if (item.BillingAccomodation.HasValue)
                                    {
                                        friBAccomTB.Text = item.PayRollAccomodation.Value.ToString("0.00##");
                                    }

                                    // Hours
                                    if (item.BillingHours.HasValue)
                                    {
                                        friBCHoursTB.Text = item.BillingHours.Value.ToString("0.0");
                                    }
                                    // Distance

                                    if (item.BillingTravelDistance.HasValue)
                                    {
                                        friBDistTB.Text = item.BillingTravelDistance.ToString();
                                    }
                                    // truck distance
                                    if (item.TruckDistance.HasValue)
                                    {
                                        friTruckDistTB.Text = item.TruckDistance.ToString();
                                    }
                                    // Misc

                                    if (item.BillingMisc.HasValue)
                                    {
                                        friBMiscTB.Text = item.BillingMisc.Value.ToString("0.00##");
                                    }
                                    friCRemarksTB.Text = item.Remarks;
                                }
                                else if (item.Day == "Saturday" && item.Classification == classification && item.Activity == activity)
                                {
                                    // Accomodation
                                    if (item.BillingAccomodation.HasValue)
                                    {
                                        satBAccomTB.Text = item.PayRollAccomodation.Value.ToString("0.00##");
                                    }

                                    // Hours
                                    if (item.BillingHours.HasValue)
                                    {
                                        satBCHoursTB.Text = item.BillingHours.Value.ToString("0.0");
                                    }
                                    // Distance
                                    if (item.BillingTravelDistance.HasValue)
                                    {
                                        satBDistTB.Text = item.BillingTravelDistance.ToString();
                                    }
                                    // truck distance
                                    if (item.TruckDistance.HasValue)
                                    {
                                        satTruckDistTB.Text = item.TruckDistance.ToString();
                                    }
                                    // Misc
                                    if (item.BillingMisc.HasValue)
                                    {
                                        satBMiscTB.Text = item.BillingMisc.Value.ToString("0.00##");
                                    }
                                    satCRemarksTB.Text = item.Remarks;
                                }
                            }
                        }
                    }
                }
                // Non-Chargeable
                else if (expense != string.Empty && expense != null || hours != string.Empty && hours != null)
                {
                    // activate corresponding accordion panel
                    aID = 2;
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "toggleAccordion", "accordionToggle();", true);

                    // set the dropdownlists and display the different panels accordingly
                    if (hours != string.Empty)
                    {
                        specifyDDL.SelectedValue = hours;
                        NonChargeHoursPanel.Visible = true;
                    } if (expense != string.Empty)
                    {
                        detailsDDL.SelectedValue = expense;
                        NonChargeExpensesPanel.Visible = true;
                    }

                    // adjust buttons
                    nonChargeBtn.Visible = false;
                    updateNonChargeableBtn.Visible = true;

                    List<ManagerNonChargeable> nonCh = tm.getManagerNonChargeableForTimesheetID(timesheet, expense, hours);
                    if (nonCh != null)
                    {
                        // populate the non chargeable text fields based on the expense and type of hours selected
                        foreach (var item in nonCh)
                        {
                            if (item.Day == "Sunday")
                            {
                                if (item.Accomodations.HasValue)
                                {
                                    sunNCAccomTB.Text = item.Accomodations.Value.ToString("0.####");
                                }
                                sunNCHoursTB.Text = item.Hours.Value.ToString("0.#");
                                sunNCDistanceTB.Text = item.Distance.ToString();
                                if (item.Misc.HasValue)
                                {
                                    sunNCMiscTB.Text = item.Misc.Value.ToString("0.####");
                                }

                                sunNCRemarksTB.Text = item.Remarks;
                            }
                            else if (item.Day == "Monday")
                            {
                                if (item.Accomodations.HasValue)
                                {
                                    monNCAccomTB.Text = item.Accomodations.Value.ToString("0.####");
                                }
                                monNCHoursTB.Text = item.Hours.Value.ToString("0.#");
                                monNCDistanceTB.Text = item.Distance.ToString();
                                if (item.Misc.HasValue)
                                {
                                    monNCMiscTB.Text = item.Misc.Value.ToString("0.####");
                                }

                                monNCRemarksTB.Text = item.Remarks;
                            }
                            else if (item.Day == "Tuesday")
                            {
                                if (item.Accomodations.HasValue)
                                {
                                    tuesNCAccomTB.Text = item.Accomodations.Value.ToString("0.####");
                                }
                                tuesNCHoursTB.Text = item.Hours.Value.ToString("0.#");
                                tuesNCDistanceTB.Text = item.Distance.ToString();
                                if (item.Misc.HasValue)
                                {
                                    tuesNCMiscTB.Text = item.Misc.Value.ToString("0.####");
                                }

                                tuesNCRemarksTB.Text = item.Remarks;
                            }
                            else if (item.Day == "Wednesday")
                            {
                                if (item.Accomodations.HasValue)
                                {
                                    wedsNCAccomTB.Text = item.Accomodations.Value.ToString("0.####");
                                }
                                wedsNCHoursTB.Text = item.Hours.Value.ToString("0.#");
                                wedsNCDistanceTB.Text = item.Distance.ToString();
                                if (item.Misc.HasValue)
                                {
                                    wedsNCMiscTB.Text = item.Misc.Value.ToString("0.####");
                                }
                                wedsNCRemarksTB.Text = item.Remarks;
                            }
                            else if (item.Day == "Thursday")
                            {
                                if (item.Accomodations.HasValue)
                                {
                                    thursNCAccomTB.Text = item.Accomodations.Value.ToString("0.####");
                                }
                                thursNCHoursTB.Text = item.Hours.Value.ToString("0.#");
                                thursNCDistanceTB.Text = item.Distance.ToString();
                                if (item.Misc.HasValue)
                                {
                                    thursNCMiscTB.Text = item.Misc.Value.ToString("0.####");
                                }
                                thursNCRemarksTB.Text = item.Remarks;
                            }
                            else if (item.Day == "Friday")
                            {
                                if (item.Accomodations.HasValue)
                                {
                                    friNCAccomTB.Text = item.Accomodations.Value.ToString("0.####");
                                }
                                friNCHoursTB.Text = item.Hours.Value.ToString("0.#");
                                friNCDistanceTB.Text = item.Distance.ToString();
                                if (item.Misc.HasValue)
                                {
                                    friNCMiscTB.Text = item.Misc.Value.ToString("0.####");
                                }
                                friNCRemarksTB.Text = item.Remarks;
                            }
                            else if (item.Day == "Saturday")
                            {
                                if (item.Accomodations.HasValue)
                                {
                                    satNCAccomTB.Text = item.Accomodations.Value.ToString("0.####");
                                }
                                satNCHoursTB.Text = item.Hours.Value.ToString("0.#");
                                satNCDistanceTB.Text = item.Distance.ToString();
                                satNCRemarksTB.Text = item.Remarks;
                                if (item.Misc.HasValue)
                                {
                                    satNCMiscTB.Text = item.Misc.Value.ToString("0.####");
                                }
                            }
                        }
                    }
                }
            }
        }
        // check to see if weekending is specified update the gridviews
        if (weekEndingTB.Text != string.Empty)
        {
            // re-populate the dates
            ScriptManager.RegisterStartupScript(this, this.GetType(), "populate", "populate();", true);
            // Get the timesheet id for the users whos timesheet we are managing
            string username = tm.getUsernameForEmpNo(empIdTB.Text);
            int id = tm.idForDate(weekEndingTB.Text, tm.idForUsername(username));

            if (id != 0)
            {
                timesheetID = id;
                HiddenField1.Value = id.ToString();
                // update totals
                updateTimeSheetTotals();
                summaryGV.DataBind();
            }
        }
        // populate the predictive text arrays
        projectNoDB = tm.getProjectNumbers();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        TimesheetManager tm = new TimesheetManager();
        dbData = tm.getData();

        if (!IsPostBack)
        {
            statusDDL.DataSource = tm.getTimesheetStatus();
            statusDDL.DataBind();

            branchesDDL.DataSource = tm.getBranches();
            string username = Membership.GetUser().UserName;
            User user = tm.getEmployeeForId(username);
            //branchesDDL.SelectedValue = user.branch;
            branchesDDL.DataBind();
            datesDDL.DataSource = tm.getDates();
            datesDDL.DataBind();

            // check query strings for form data to be populated
            if (Request.QueryString.Count > 0)
            {
                if (Request.QueryString["reject"] == "reject")
                {
                    TimeSheet ts = tm.getTimesheetForID(Convert.ToInt32(Request.QueryString["time"]));
                    if (ts != null)
                    {
                        tm.updateTimeSheet(ts.TimeSheetId, ts.TotalHours.Value, ts.TotalDistance.Value, ts.TotalTruck.Value, ts.TotalExpenses.Value, "Rejected", ts.EmployeeComments, ts.ManagerComments);
                        statusDDL.SelectedValue = "Rejected";
                        statusDDL.DataBind();
                        GridView1.DataBind();
                    }
                }
                else if (Request.QueryString["sync"] == "sync")
                {
                    // create a connection to the database
                    SqlConnection conn = new SqlConnection("data source=USER;initial catalog=TIMESHEET_SYNC;user id=sa;password=PMLtest;multipleactiveresultsets=True");

                    int id = Convert.ToInt32(Request.QueryString["time"]);
                    petomaccallumModel.TimeSheet t = tm.getTimesheetForID(id);
                    if (t != null)
                    {
                        conn.Open();
                        SqlCommand tsStmt = conn.CreateCommand();
                        tsStmt.CommandText =
                        "INSERT INTO TimeSheet(TimeSheetId,TotalHours,TotalDistance, TotalExpenses, EmpNo, WeekEnding, ApprovedBy,EmployeeName, ManagerComments,EmployeeComments) VALUES(@TimeSheetId,@TotalHours, @TotalDistance, @TotalExpenses, @EmpNo, @WeekEnding, @ApprovedBy,@EmployeeName, @ManagerComments, @EmployeeComments)";

                        tsStmt.Parameters.AddWithValue("@TimeSheetId", t.TimeSheetId);
                        tsStmt.Parameters.AddWithValue("@TotalHours", t.TotalHours);
                        tsStmt.Parameters.AddWithValue("@TotalDistance", t.TotalDistance);
                        tsStmt.Parameters.AddWithValue("@TotalExpenses", t.TotalExpenses);
                        // get emp no
                        User emp = tm.getEmployeeForId(t.EmployeeId);

                        tsStmt.Parameters.AddWithValue("@EmpNo", emp.empNo);
                        tsStmt.Parameters.AddWithValue("@WeekEnding", t.WeekEnding);
                        tsStmt.Parameters.AddWithValue("@ApprovedBy", t.ApprovedBy);
                        tsStmt.Parameters.AddWithValue("@EmployeeName", t.EmployeeName);
                        tsStmt.Parameters.AddWithValue("@ManagerComments", t.ManagerComments);

                        tsStmt.Parameters.AddWithValue("@EmployeeComments", t.EmployeeComments);

                        tsStmt.ExecuteNonQuery();
                        tsStmt.Dispose();

                        // get manager chargeable data and map it
                        List<ManagerChargeable> mData = tm.getManagerChargeableForID(id);

                        if (mData != null)
                        {
                            // TODO: map the managerChargeable to the local db
                            try
                            {

                                // for each ManagerChargeable row for the timesheet, insert to temporary database
                                foreach (ManagerChargeable item in mData)
                                {
                                    SqlCommand stmt = conn.CreateCommand();
                                    //get projnum, name, client

                                    //get projnum, name, client
                                    stmt.CommandText =
                                    "INSERT INTO Chargeable(TimeSheetId,Day,PayRollHours,PayRollTravelDistance,PayRollAccomodation,PayRollMisc,BillingHours,BillingTravelDistance,BillingAccomodation,BillingMisc,ProjectNo,Classification,Activity,Remarks,BillingTruckDistance)" +
                                    "VALUES(@TimeSheetId,@Day,@PayRollHours,@PayRollTravelDistance, @PayRollAccomodation ,@PayRollMisc, @BillingHours, @BillingTravelDistance, @BillingAccomodation, @BillingMisc, @ProjectNo, @Classification, @Activity, @Remarks,@BillingTruckDistance)";
                                    stmt.Parameters.AddWithValue("@TimeSheetId", item.TimeSheetId);
                                    stmt.Parameters.AddWithValue("@Day", item.Day);
                                    // payroll
                                    if (item.PayRollHours.HasValue)
                                    {
                                        stmt.Parameters.AddWithValue("@PayRollHours", item.PayRollHours.Value);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@PayRollHours", 0);
                                    }
                                    if (item.PayRollTravelDistance.HasValue)
                                    {
                                        stmt.Parameters.AddWithValue("@PayRollTravelDistance", item.PayRollTravelDistance.Value);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@PayRollTravelDistance", 0);
                                    }
                                    if (item.PayRollAccomodation.HasValue)
                                    {
                                        stmt.Parameters.AddWithValue("@PayRollAccomodation", item.PayRollAccomodation.Value);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@PayRollAccomodation", 0);
                                    }
                                    if (item.PayRollMisc.HasValue)
                                    {
                                        stmt.Parameters.AddWithValue("@PayRollMisc", item.PayRollMisc.Value);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@PayRollMisc", 0);
                                    }

                                    // billing
                                    if (item.BillingHours.HasValue)
                                    {
                                        stmt.Parameters.AddWithValue("@BillingHours", item.BillingHours.Value);
                                    }
                                    if (item.BillingTravelDistance.HasValue)
                                    {
                                        stmt.Parameters.AddWithValue("@BillingTravelDistance", item.BillingTravelDistance.Value);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@BillingTravelDistance", 0);
                                    }

                                    if (item.BillingAccomodation.HasValue)
                                    {
                                        stmt.Parameters.AddWithValue("@BillingAccomodation", item.BillingAccomodation.Value);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@BillingAccomodation", 0);
                                    }

                                    if (item.BillingMisc.HasValue)
                                    {
                                        stmt.Parameters.AddWithValue("@BillingMisc", item.BillingMisc.Value);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@BillingMisc", 0);
                                    }
                                    // get project number
                                    string no = tm.getProjectNoForID(item.ProjectId);
                                    stmt.Parameters.AddWithValue("@ProjectNo", no);
                                    stmt.Parameters.AddWithValue("@Classification", item.Classification);
                                    stmt.Parameters.AddWithValue("@Activity", item.Activity);
                                    if (item.Remarks != null)
                                    {
                                        stmt.Parameters.AddWithValue("@Remarks", item.Remarks);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@Remarks", " ");
                                    }

                                    if (item.TruckDistance.HasValue)
                                    {
                                        stmt.Parameters.AddWithValue("@BillingTruckDistance", item.TruckDistance);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@BillingTruckDistance", 0);
                                    }

                                    stmt.ExecuteNonQuery();
                                    stmt.Dispose();
                                    stmt = null;
                                }
                            }
                            catch (Exception)
                            {
                                throw;
                            }
                        }

                        List<ManagerNonChargeable> nData = tm.getManagerNonChargeableForID(id);

                        if (nData != null)
                        {
                            try
                            {

                                // save each row of non chargeable data to the sync database
                                foreach (ManagerNonChargeable item in nData)
                                {

                                    SqlCommand stmt = conn.CreateCommand();

                                    stmt.CommandText =
                                   "INSERT INTO NonChargeable (TimeSheetId,Hours,Distance,Accomodations,Misc,TypeHours,TypeExpense,Day,Remarks)VALUES(@TimeSheetId,@Hours, @Distance, @Accomodations, @Misc, @TypeHours, @TypeExpense, @Day, @Remarks)";

                                    stmt.Parameters.AddWithValue("@TimeSheetId", t.TimeSheetId);

                                    if (item.Hours.HasValue)
                                    {
                                        stmt.Parameters.AddWithValue("@Hours", item.Hours.Value);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@Hours", 0);
                                    }

                                    if (item.Distance.HasValue)
                                    {
                                        stmt.Parameters.AddWithValue("@Distance", item.Distance.Value);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@Distance", 0);
                                    }

                                    if (item.Accomodations.HasValue)
                                    {
                                        stmt.Parameters.AddWithValue("@Accomodations", item.Accomodations.Value);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@Accomodations", 0);
                                    }
                                    if (item.Misc.HasValue)
                                    {
                                        stmt.Parameters.AddWithValue("@Misc", item.Misc.Value);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@Misc", 0);
                                    }

                                    if (item.TypeHours != null)
                                    {
                                        stmt.Parameters.AddWithValue("@TypeHours", item.TypeHours);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@TypeHours", " ");
                                    }
                                    if (item.TypeExpense != null)
                                    {
                                        stmt.Parameters.AddWithValue("@TypeExpense", item.TypeExpense);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@TypeExpense", " ");

                                    }
                                    stmt.Parameters.AddWithValue("@Day", item.Day);

                                    if (item.Remarks != null)
                                    {
                                        stmt.Parameters.AddWithValue("@Remarks", item.Remarks);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@Remarks", " ");
                                    }
                                    stmt.ExecuteNonQuery();
                                    stmt.Dispose();
                                    stmt = null;
                                }
                            }
                            catch (Exception)
                            {
                                throw;
                            }
                        }

                        // Synchronize the Lab and Density Tests
                        List<ManagerTest> tests = tm.getManagerTestsForTimeSheet(id);
                        if (tests != null)
                        {
                            try
                            {
                                foreach (ManagerTest item in tests)
                                {
                                    SqlCommand stmt = conn.CreateCommand();
                                    stmt.CommandText = "INSERT INTO ManagerTest(LabTest,DensityTest,TimeSheetId,Day) VALUES(@LabTest,@DensityTest,@TimeSheetId,@Day)";
                                    if (item.LabTest.HasValue)
                                    {
                                        stmt.Parameters.AddWithValue("@LabTest", item.LabTest);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@LabTest", 0);
                                    }
                                    if (item.DensityTest.HasValue)
                                    {
                                        stmt.Parameters.AddWithValue("@DensityTest", item.DensityTest);
                                    }
                                    else
                                    {
                                        stmt.Parameters.AddWithValue("@DensityTest", 0);
                                    }

                                    if (item.TimeSheetId > 0)
                                    {
                                        stmt.Parameters.AddWithValue("@TimeSheetId", item.TimeSheetId);
                                    }

                                    if (item.Day != string.Empty)
                                    {
                                        stmt.Parameters.AddWithValue("@Day", item.Day);
                                    }

                                    stmt.ExecuteNonQuery();
                                    stmt.Dispose();
                                    stmt = null;
                                }
                            }
                            catch (Exception)
                            {

                                throw;
                            }

                        }

                        // update timesheet
                        tm.updateTimeSheet(t.TimeSheetId, t.TotalHours.Value, t.TotalDistance.Value, t.TotalTruck.Value, t.TotalExpenses.Value, "Synchronized", " ", " ");
                        conn.Close();
                    }
                }
            }
        }
    }