private void UpdateDatabase()
        {
            DB.Open();
            DB.BeginTransaction();
            try
            {
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int loginId = Convert.ToInt32(Session["loginID"]);

                if (cbxEndConfirm.Checked)
                {
                    ProjectCostingSheetAddBasicInformation projectCostingSheetAddBasicInformation = new ProjectCostingSheetAddBasicInformation(projectCostingSheetAddTDS);
                    int costingSheetId = projectCostingSheetAddBasicInformation.Save(companyId);
                    hdfCostingSheetId.Value = costingSheetId.ToString();

                    // Save costs information
                    ProjectCostingSheetAddLabourHoursInformation projectCostingSheetAddLabourHoursInformation = new ProjectCostingSheetAddLabourHoursInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddLabourHoursInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddUnitsInformation projectCostingSheetAddUnitsInformation = new ProjectCostingSheetAddUnitsInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddUnitsInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddMaterialsInformation projectCostingSheetAddMaterialsInformation = new ProjectCostingSheetAddMaterialsInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddMaterialsInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddSubcontractorsInformation projectCostingSheetAddSubcontractorsInformation = new ProjectCostingSheetAddSubcontractorsInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddSubcontractorsInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddOtherCostsInformation projectCostingSheetAddOtherCostsInformation = new ProjectCostingSheetAddOtherCostsInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddOtherCostsInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddHotelsInformation projectCostingSheetAddHotelsInformation = new ProjectCostingSheetAddHotelsInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddHotelsInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddBondingsInformation projectCostingSheetAddBondingsInformation = new ProjectCostingSheetAddBondingsInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddBondingsInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddInsurancesInformation projectCostingSheetAddInsurancesInformation = new ProjectCostingSheetAddInsurancesInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddInsurancesInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddOtherCategoryInformation projectCostingSheetAddOtherCategoryInformation = new ProjectCostingSheetAddOtherCategoryInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddOtherCategoryInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddRevenueInformation projectCostingSheetAddRevenueInformation = new ProjectCostingSheetAddRevenueInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddRevenueInformation.Save(companyId, costingSheetId);

                    if (rbtnBeginTemplate.Checked)
                    {
                        ProjectCostingSheetTemplateInformation projectCostingSheetTemplateInformation = new ProjectCostingSheetTemplateInformation(projectCostingSheetAddTDS);
                        projectCostingSheetTemplateInformation.Save(companyId);
                    }
                }
                else
                {
                    if (cbxEndSave.Checked)
                    {
                        ProjectCostingSheetTemplateInformation projectCostingSheetTemplateInformation = new ProjectCostingSheetTemplateInformation(projectCostingSheetAddTDS);
                        projectCostingSheetTemplateInformation.Save(companyId);
                    }
                    else
                    {
                        if (rbtnBeginTemplate.Checked)
                        {
                            ProjectCostingSheetTemplateInformation projectCostingSheetTemplateInformation = new ProjectCostingSheetTemplateInformation(projectCostingSheetAddTDS);
                            projectCostingSheetTemplateInformation.Save(companyId);
                        }
                    }
                }

                DB.CommitTransaction();

                // Store datasets
                projectCostingSheetAddTDS.AcceptChanges();
                Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // STEP5 - TRUCKS EQUIPMENT INFORMATION - METHODS
        //
        private void StepTrucksEquipmentInformationIn()
        {
            // Set instruction
            Label instruction = (Label)this.Master.FindControl("lblInstruction");
            instruction.Text = "Please verify Truck / Equipment information";

            // Load
            ProjectCostingSheetAddUnitsInformation model = new ProjectCostingSheetAddUnitsInformation(projectCostingSheetAddTDS);

            if (projectCostingSheetAddTDS.UnitsInformation.Rows.Count <= 0)
            {
                ArrayList works = new ArrayList();
                if (cbxRehabAssessmentData.Checked) works.Add("Rehab Assessment");
                if (cbxFullLengthLiningData.Checked) works.Add("Full Length");
                if (cbxPointRepairData.Checked) { works.Add("Point Lining"); works.Add("Grouting"); }
                if (cbxJunctionLiningData.Checked) works.Add("Junction Lining");
                if (cbxManholeRehabData.Checked) works.Add("MH Rehab");
                if (cbxMobilizationData.Checked) works.Add("Mobilization");
                if (cbxOtherData.Checked)
                {
                    works.Add("Other");
                    works.Add("Downtime");
                    works.Add("Office");
                    works.Add("Office / Shop");
                    works.Add("R & D");
                    works.Add("Special Projects");
                    works.Add("Subcontractor");
                    works.Add("Watermain Relining");
                    works.Add("SOTA");
                }

                model.Load(works, int.Parse(hdfProjectId.Value), tkrdpFrom.SelectedDate.Value, tkrdpTo.SelectedDate.Value, int.Parse(hdfCompanyId.Value));
            }

            // Store tables
            Session.Remove("unitsInformationDummy");
            unitsInformation = (ProjectCostingSheetAddTDS.UnitsInformationDataTable)model.Table;
            Session["unitsInformation"] = unitsInformation;
            Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

            // Validate grid columns
            int projectId = Int32.Parse(hdfProjectId.Value);
            ProjectGateway projectGateway = new ProjectGateway();
            projectGateway.LoadByProjectId(projectId);

            if (projectGateway.GetCountryID(projectId) == 1) //Canada
            {
                // Units Grid
                grdUnits.Columns[11].Visible = true;
                grdUnits.Columns[12].Visible = true;
                grdUnits.Columns[13].Visible = false;
                grdUnits.Columns[14].Visible = false;

                lblUnitsTotalCosts.Text = "Total Cost (CAD) : ";
                tbxUnitsTotalCostsCAD.Visible = true;
                tbxUnitsTotalCostsUSD.Visible = false;
            }
            else
            {
                // Units Grid
                grdUnits.Columns[11].Visible = false;
                grdUnits.Columns[12].Visible = false;
                grdUnits.Columns[13].Visible = true;
                grdUnits.Columns[14].Visible = true;

                lblUnitsTotalCosts.Text = "Total Cost (USD) : ";
                tbxUnitsTotalCostsCAD.Visible = false;
                tbxUnitsTotalCostsUSD.Visible = true;
            }

            grdUnits.DataBind();
            StepTrucksEquipmentInformationProcessGrid();
        }
        protected void grdUnits_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Units Gridview, if the gridview is edition mode
            if (grdUnits.EditIndex >= 0)
            {
                grdUnits.UpdateRow(grdUnits.EditIndex, true);
            }

            // Delete Unit Cost
            int costingSheetId = (int)e.Keys["CostingSheetID"];
            string work_ = (string)e.Keys["Work_"];
            int unitId = (int)e.Keys["UnitID"];
            int refId = (int)e.Keys["RefID"];

            ProjectCostingSheetAddUnitsInformation model = new ProjectCostingSheetAddUnitsInformation(projectCostingSheetAddTDS);
            model.Delete(costingSheetId, work_, unitId, refId);

            // Store dataset
            unitsInformation = (ProjectCostingSheetAddTDS.UnitsInformationDataTable)model.Table;
            Session["unitsInformation"] = unitsInformation;
            Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

            StepTrucksEquipmentInformationProcessGrid();
        }
        protected void grdUnits_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // Validate general data
            Page.Validate("unitsEdit");
            int projectId = Int32.Parse(hdfProjectId.Value);
            ProjectGateway projectGateway = new ProjectGateway();

            if (Page.IsValid)
            {
                // Validate costs
                projectGateway.LoadByProjectId(projectId);

                if (projectGateway.GetCountryID(projectId) == 1) //Canada
                {
                    Page.Validate("unitsCadEdit");
                }
                else
                {
                    Page.Validate("unitsUsdEdit");
                }
            }

            if (Page.IsValid)
            {
                int costingSheetId = (int)e.Keys["CostingSheetID"];
                string work_ = (string)e.Keys["Work_"];
                int unitId = (int)e.Keys["UnitID"];
                int refId = (int)e.Keys["RefID"];

                int companyId = Int32.Parse(hdfCompanyId.Value);
                string unitOfMeasurement = ((DropDownList)grdUnits.Rows[e.RowIndex].Cells[0].FindControl("ddlUnitOfMeasurementUnitsEdit")).SelectedValue;
                double quantity = double.Parse(((TextBox)grdUnits.Rows[e.RowIndex].Cells[0].FindControl("tbxQuantityEdit")).Text.Trim());

                decimal costCad = 0.0M;
                decimal totalCostCad = 0.0M;
                decimal costUsd = 0.0M;
                decimal totalCostUsd = 0.0M;

                if (projectGateway.GetCountryID(projectId) == 1) //Canada
                {
                    costCad = Decimal.Parse(((TextBox)grdUnits.Rows[e.RowIndex].Cells[0].FindControl("tbxCostCADEdit")).Text.Trim());
                    totalCostCad = (costCad * decimal.Parse(quantity.ToString()));
                    totalCostCad = Decimal.Round(totalCostCad, 2);
                }
                else
                {
                    costUsd = Decimal.Parse(((TextBox)grdUnits.Rows[e.RowIndex].Cells[0].FindControl("tbxCostUSDEdit")).Text.Trim());
                    totalCostUsd = (costUsd * decimal.Parse(quantity.ToString()));
                    totalCostUsd = Decimal.Round(totalCostUsd, 2);
                }

                LFSLive.DA.FleetManagement.Units.UnitsGateway u = new LiquiForce.LFSLive.DA.FleetManagement.Units.UnitsGateway();
                u.LoadByUnitId(unitId, companyId);
                string unitDescription = u.GetDescription(unitId);
                string unitCode = u.GetUnitCode(unitId);

                DateTime startDate = ((RadDatePicker)grdUnits.Rows[e.RowIndex].Cells[0].FindControl("tkrdpStartDateEdit")).SelectedDate.Value;
                DateTime endDate = ((RadDatePicker)grdUnits.Rows[e.RowIndex].Cells[0].FindControl("tkrdpEndDateEdit")).SelectedDate.Value;

                string workFunctionConcat = "";
                string function_ = "";
                workFunctionConcat = ((DropDownList)grdUnits.Rows[e.RowIndex].Cells[0].FindControl("ddlWorkFunctionEdit")).SelectedValue;
                if (workFunctionConcat != "(Select)")
                {
                    string[] workFunction = workFunctionConcat.ToString().Trim().Split('.');
                    function_ = workFunction[1].Trim();
                }

                // Update data
                ProjectCostingSheetAddUnitsInformation model = new ProjectCostingSheetAddUnitsInformation(projectCostingSheetAddTDS);
                model.Update(costingSheetId, work_, unitId, refId, unitOfMeasurement, quantity, costCad, totalCostCad, costUsd, totalCostUsd, false, companyId, unitCode, unitDescription, startDate, endDate, workFunctionConcat, function_);

                // Store dataset
                unitsInformation = (ProjectCostingSheetAddTDS.UnitsInformationDataTable)model.Table;
                Session["unitsInformation"] = unitsInformation;
                Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

                StepTrucksEquipmentInformationProcessGrid();
            }
            else
            {
                e.Cancel = true;
            }
        }
        // /////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        // STEP5 - TRUCKS EQUIPMENT INFORMATION
        //
        // ////////////////////////////////////////////////////////////////////////
        // STEP5 - TRUCKS EQUIPMENT INFORMATION - EVENTS
        //
        protected void grdUnits_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Add":
                    // Units Gridview, if the gridview is edition mode
                    if (grdUnits.EditIndex >= 0)
                    {
                        grdUnits.UpdateRow(grdUnits.EditIndex, true);
                    }

                    // Validate general data
                    Page.Validate("unitsNew");
                    int projectId = Int32.Parse(hdfProjectId.Value);
                    ProjectGateway projectGateway = new ProjectGateway();

                    if (Page.IsValid)
                    {
                        // Validate costs
                        projectGateway.LoadByProjectId(projectId);

                        if (projectGateway.GetCountryID(projectId) == 1) //Canada
                        {
                            Page.Validate("unitsCadNew");
                        }
                        else
                        {
                            Page.Validate("unitsUsdNew");
                        }
                    }

                    if (Page.IsValid)
                    {
                        int companyId = Int32.Parse(hdfCompanyId.Value);
                        string typeOfWork = ((DropDownList)grdUnits.FooterRow.FindControl("ddlTypeOfWork_New")).SelectedValue;
                        int unitId = Int32.Parse(((DropDownList)grdUnits.FooterRow.FindControl("ddlUnitCodeNew")).SelectedValue);
                        string unitCode = ((DropDownList)grdUnits.FooterRow.FindControl("ddlUnitCodeNew")).SelectedItem.Text;
                        string unitOfMeasurement = ((DropDownList)grdUnits.FooterRow.FindControl("ddlUnitOfMeasurementUnitsNew")).SelectedValue;
                        double quantity = double.Parse(((TextBox)grdUnits.FooterRow.FindControl("tbxQuantityNew")).Text.Trim());

                        decimal costCad = 0.0M;
                        decimal totalCostCad = 0.0M;
                        decimal costUsd = 0.0M;
                        decimal totalCostUsd = 0.0M;

                        if (projectGateway.GetCountryID(projectId) == 1) //Canada
                        {
                            costCad = Decimal.Parse(((TextBox)grdUnits.FooterRow.FindControl("tbxCostCADNew")).Text.Trim());
                            totalCostCad = (costCad * decimal.Parse(quantity.ToString()));
                            totalCostCad = Decimal.Round(totalCostCad, 2);
                        }
                        else
                        {
                            costUsd = Decimal.Parse(((TextBox)grdUnits.FooterRow.FindControl("tbxCostUSDNew")).Text.Trim());
                            totalCostUsd = (costUsd * decimal.Parse(quantity.ToString()));
                            totalCostUsd = Decimal.Round(totalCostUsd, 2);
                        }

                        LFSLive.DA.FleetManagement.Units.UnitsGateway u = new LiquiForce.LFSLive.DA.FleetManagement.Units.UnitsGateway();
                        u.LoadByUnitId(unitId, companyId);
                        string unitDescription = u.GetDescription(unitId);

                        DateTime startDate = ((RadDatePicker)grdUnits.FooterRow.FindControl("tkrdpStartDateNew")).SelectedDate.Value;
                        DateTime endDate = ((RadDatePicker)grdUnits.FooterRow.FindControl("tkrdpEndDateNew")).SelectedDate.Value;

                        string workFunctionConcat = "";
                        string function_ = "";
                        workFunctionConcat = ((DropDownList)grdUnits.FooterRow.FindControl("ddlWorkFunctionNew")).SelectedValue;
                        if (workFunctionConcat != "(Select)")
                        {
                            string[] workFunction = workFunctionConcat.ToString().Trim().Split('.');
                            function_ = workFunction[1].Trim();
                        }

                        ProjectCostingSheetAddUnitsInformation model = new ProjectCostingSheetAddUnitsInformation(projectCostingSheetAddTDS);
                        model.Insert(0, typeOfWork, unitId, unitOfMeasurement, quantity, costCad, totalCostCad, costUsd, totalCostUsd, false, companyId, unitCode, unitDescription, startDate, endDate, workFunctionConcat, function_);

                        Session.Remove("unitsInformationDummy");
                        unitsInformation = (ProjectCostingSheetAddTDS.UnitsInformationDataTable)model.Table;
                        Session["unitsInformation"] = unitsInformation;
                        Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

                        grdUnits.DataBind();

                        StepTrucksEquipmentInformationProcessGrid();
                    }
                    break;
            }
        }