private void UpdateDatabase()
        {
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int costingSheetId = Int32.Parse(hdfCostingSheetId.Value);

            DB.Open();
            DB.BeginTransaction();
            try
            {
                ProjectCostingSheetInformationBasicInformation projectCostingSheetInformationBasicInformation = new ProjectCostingSheetInformationBasicInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationBasicInformation.Save(companyId);

                // Save costs information
                ProjectCostingSheetInformationLabourHoursInformation projectCostingSheetInformationLabourHoursInformation = new ProjectCostingSheetInformationLabourHoursInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationLabourHoursInformation.Save(companyId, costingSheetId);

                ProjectCostingSheetInformationUnitsInformation projectCostingSheetInformationUnitsInformation = new ProjectCostingSheetInformationUnitsInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationUnitsInformation.Save(companyId, costingSheetId);

                ProjectCostingSheetInformationMaterialsInformation projectCostingSheetInformationMaterialsInformation = new ProjectCostingSheetInformationMaterialsInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationMaterialsInformation.Save(companyId, costingSheetId);

                ProjectCostingSheetInformationOtherCostsInformation projectCostingSheetInformationOtherCostsInformation = new ProjectCostingSheetInformationOtherCostsInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationOtherCostsInformation.Save(companyId, costingSheetId);

                ProjectCostingSheetInformationSubcontractorsInformation projectCostingSheetInformationSubcontractorsInformation = new ProjectCostingSheetInformationSubcontractorsInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationSubcontractorsInformation.Save(companyId, costingSheetId);

                ProjectCostingSheetInformationHotelsInformation projectCostingSheetInformationHotelsInformation = new ProjectCostingSheetInformationHotelsInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationHotelsInformation.Save(companyId, costingSheetId);

                ProjectCostingSheetInformationBondingsInformation projectCostingSheetInformationBondingsInformation = new ProjectCostingSheetInformationBondingsInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationBondingsInformation.Save(companyId, costingSheetId);

                ProjectCostingSheetInformationInsurancesInformation projectCostingSheetInformationInsurancesInformation = new ProjectCostingSheetInformationInsurancesInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationInsurancesInformation.Save(companyId, costingSheetId);

                ProjectCostingSheetInformationOtherCategoryInformation projectCostingSheetInformationOtherCategoryInformation = new ProjectCostingSheetInformationOtherCategoryInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationOtherCategoryInformation.Save(companyId, costingSheetId);

                DB.CommitTransaction();

                // Store datasets
                projectCostingSheetInformationTDS.AcceptChanges();
                Session["lfsProjectTDS"] = projectTDS;
                Session["projectCostingSheetInformationTDS"] = projectCostingSheetInformationTDS;
                Session["labourHoursInformation"] = projectCostingSheetInformationTDS.LabourHoursInformation;
                Session["unitsInformation"] = projectCostingSheetInformationTDS.UnitsInformation;
                Session["subcontractorsInformation"] = projectCostingSheetInformationTDS.SubcontractorsInformation;
                Session["materialsInformation"] = projectCostingSheetInformationTDS.MaterialsInformation;
                Session["otherCostsInformation"] = projectCostingSheetInformationTDS.OtherCostsInformation;

                Session["hotelssInformation"] = projectCostingSheetInformationTDS.HotelsInformation;
                Session["bondingsInformation"] = projectCostingSheetInformationTDS.BondingsInformation;
                Session["insurancesInformation"] = projectCostingSheetInformationTDS.InsurancesInformation;
                Session["otherCategoryInformation"] = projectCostingSheetInformationTDS.OtherCategoryInformation;
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        protected void grdSubcontractors_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Subcontractors Gridview, if the gridview is edition mode
            if (grdSubcontractors.EditIndex >= 0)
            {
                grdSubcontractors.UpdateRow(grdSubcontractors.EditIndex, true);
            }

            // Delete subcontractor
            int costingSheetId = (int)e.Keys["CostingSheetID"];
            int subcontractorId = (int)e.Keys["SubcontractorID"];
            int refId = (int)e.Keys["RefID"];

            ProjectCostingSheetInformationSubcontractorsInformation model = new ProjectCostingSheetInformationSubcontractorsInformation(projectCostingSheetInformationTDS);
            model.Delete(costingSheetId, subcontractorId, refId);

            // Store dataset
            subcontractorsInformation = (ProjectCostingSheetInformationTDS.SubcontractorsInformationDataTable)model.Table;
            Session["subcontractorsInformation"] = subcontractorsInformation;
            Session["projectCostingSheetInformationTDS"] = projectCostingSheetInformationTDS;

            StepSubcontractorsInformationProcessGrid();
        }
        protected void grdSubcontractors_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // Validate general data
            Page.Validate("subcontractorsEdit");
            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("subcontractorsCadEdit");
                }
                else
                {
                    Page.Validate("subcontractorsUsdEdit");
                }
            }

            if (Page.IsValid)
            {
                int costingSheetId = (int)e.Keys["CostingSheetID"];
                int subcontractorId = (int)e.Keys["SubcontractorID"];
                int refId = (int)e.Keys["RefID"];

                int companyId = Int32.Parse(hdfCompanyId.Value);
                string unitOfMeasurement = ((DropDownList)grdSubcontractors.Rows[e.RowIndex].Cells[0].FindControl("ddlUnitOfMeasurementSubcontractorsEdit")).SelectedValue;
                string subcontractor = ((TextBox)grdSubcontractors.Rows[e.RowIndex].Cells[0].FindControl("tbxSubcontractorEdit")).Text;
                double quantity = double.Parse(((TextBox)grdSubcontractors.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)grdSubcontractors.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)grdSubcontractors.Rows[e.RowIndex].Cells[0].FindControl("tbxCostUSDEdit")).Text.Trim());
                    totalCostUsd = (costUsd * decimal.Parse(quantity.ToString()));
                    totalCostUsd = Decimal.Round(totalCostUsd, 2);
                }

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

                // Update data
                ProjectCostingSheetInformationSubcontractorsInformation model = new ProjectCostingSheetInformationSubcontractorsInformation(projectCostingSheetInformationTDS);
                model.Update(costingSheetId, subcontractorId, refId, unitOfMeasurement, quantity, costCad, totalCostCad, costUsd, totalCostUsd, false, companyId, subcontractor, startDate, endDate);

                // Store dataset
                subcontractorsInformation = (ProjectCostingSheetInformationTDS.SubcontractorsInformationDataTable)model.Table;
                Session["subcontractorsInformation"] = subcontractorsInformation;
                Session["projectCostingSheetInformationTDS"] = projectCostingSheetInformationTDS;

                StepSubcontractorsInformationProcessGrid();
            }
            else
            {
                e.Cancel = true;
            }
        }
        protected void grdSubcontractors_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Add":
                    // Subcontractors Gridview, if the gridview is edition mode
                    if (grdSubcontractors.EditIndex >= 0)
                    {
                        grdSubcontractors.UpdateRow(grdSubcontractors.EditIndex, true);
                    }

                    // Validate general data
                    Page.Validate("subcontractorsNew");
                    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("subcontractorsCadNew");
                        }
                        else
                        {
                            Page.Validate("subcontractorsUsdNew");
                        }
                    }

                    if (Page.IsValid)
                    {
                        int companyId = Int32.Parse(hdfCompanyId.Value);
                        int subcontractorId = Int32.Parse(((DropDownList)grdSubcontractors.FooterRow.FindControl("ddlSubcontractorNew")).SelectedValue);
                        string unitOfMeasurement = ((DropDownList)grdSubcontractors.FooterRow.FindControl("ddlUnitOfMeasurementSubcontractorsNew")).SelectedValue;
                        string subcontractor = ((DropDownList)grdSubcontractors.FooterRow.FindControl("ddlSubcontractorNew")).SelectedItem.Text;
                        double quantity = double.Parse(((TextBox)grdSubcontractors.FooterRow.FindControl("tbxQuantityNew")).Text.Trim());
                        string comment = ""; if (!string.IsNullOrEmpty(((TextBox)grdSubcontractors.FooterRow.FindControl("tbxCommentNew")).Text.Trim())) comment = ((TextBox)grdSubcontractors.FooterRow.FindControl("tbxCommentNew")).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)grdSubcontractors.FooterRow.FindControl("tbxCostCADNew")).Text.Trim());
                            totalCostCad = (costCad * decimal.Parse(quantity.ToString()));
                            totalCostCad = Decimal.Round(totalCostCad, 2);
                        }
                        else
                        {
                            costUsd = Decimal.Parse(((TextBox)grdSubcontractors.FooterRow.FindControl("tbxCostUSDNew")).Text.Trim());
                            totalCostUsd = (costUsd * decimal.Parse(quantity.ToString()));
                            totalCostUsd = Decimal.Round(totalCostUsd, 2);
                        }

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

                        ProjectCostingSheetInformationSubcontractorsInformation model = new ProjectCostingSheetInformationSubcontractorsInformation(projectCostingSheetInformationTDS);
                        model.Insert(0, subcontractorId, unitOfMeasurement, quantity, costCad, totalCostCad, costUsd, totalCostUsd, false, companyId, subcontractor, startDate, endDate, comment);

                        Session.Remove("subcontractorsInformationDummy");
                        subcontractorsInformation = (ProjectCostingSheetInformationTDS.SubcontractorsInformationDataTable)model.Table;
                        Session["subcontractorsInformation"] = subcontractorsInformation;
                        Session["projectCostingSheetInformationTDS"] = projectCostingSheetInformationTDS;

                        grdSubcontractors.DataBind();

                        StepSubcontractorsInformationProcessGrid();
                    }
                    break;
            }
        }