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 grdTeamMembers_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Team member Gridview, if the gridview is edition mode
            if (grdTeamMembers.EditIndex >= 0)
            {
                grdTeamMembers.UpdateRow(grdTeamMembers.EditIndex, true);
            }

            // Add New Team Member
            int costingSheetId = (int)e.Keys["CostingSheetID"];
            string work_ = (string)e.Keys["Work_"];
            int employeeId = (int)e.Keys["EmployeeID"];
            int refId = (int)e.Keys["RefID"];

            ProjectCostingSheetInformationLabourHoursInformation model = new ProjectCostingSheetInformationLabourHoursInformation(projectCostingSheetInformationTDS);

            // Delete cost
            model.Delete(costingSheetId, work_, employeeId, refId);

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

            StepLabourHoursInformationProcessGrid();
        }
        protected void grdTeamMembers_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int projectId = Int32.Parse(hdfProjectId.Value);
            ProjectGateway projectGateway = new ProjectGateway();
            projectGateway.LoadByProjectId(projectId);

            if (projectGateway.GetCountryID(projectId) == 1) //Canada
            {
                Page.Validate("labourHoursCadEdit");
            }
            else
            {
                Page.Validate("labourHoursUsdEdit");
            }
            if (Page.IsValid)
            {
                Page.Validate("labourHoursEdit");
                if (Page.IsValid)
                {
                    int costingSheetId = (int)e.Keys["CostingSheetID"];
                    string work_ = (string)e.Keys["Work_"];
                    int employeeId = (int)e.Keys["EmployeeID"];
                    int companyId = Int32.Parse(hdfCompanyId.Value);
                    int refId = (int)e.Keys["RefID"];

                    string name = ((TextBox)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("tbxTeamMemberEdit")).Text;
                    string unitOfMeasurementLH = ((DropDownList)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("ddlUnitOfMeasurementLHEdit")).SelectedValue;
                    double lHQuantity = double.Parse(((TextBox)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("tbxLHQtyEdit")).Text.Trim());
                    string unitOfMeasurementMeals = ((DropDownList)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("ddlUnitsOfMeasurementLHMealsEdit")).SelectedValue;
                    int? mealsQuantity = null; if (((TextBox)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("tbxMealsQtyEdit")).Text.Trim() != "") mealsQuantity = Int32.Parse(((TextBox)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("tbxMealsQtyEdit")).Text.Trim());
                    string unitOfMeasurementMotel = ((DropDownList)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("ddlUnitsOfMeasurementLHMotelEdit")).SelectedValue;
                    int? motelQuantity = null; if (((TextBox)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("tbxMotelQtyEdit")).Text.Trim() != "") motelQuantity = Int32.Parse(((TextBox)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("tbxMotelQtyEdit")).Text.Trim());

                    decimal lhCostCad = 0;
                    decimal? mealsCostCad = null;
                    decimal? motelCostCad = null;
                    decimal totalCostCad = 0;
                    decimal lhCostUsd = 0;
                    decimal? mealsCostUsd = null;
                    decimal? motelCostUsd = null;
                    decimal totalCostUsd = 0;
                    if (projectGateway.GetCountryID(projectId) == 1) //Canada
                    {
                        lhCostCad = Decimal.Parse(((TextBox)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("tbxLHCostCADEdit")).Text.Trim());
                        if (((TextBox)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("tbxMealsCostCADEdit")).Text.Trim() != "") mealsCostCad = Decimal.Parse(((TextBox)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("tbxMealsCostCADEdit")).Text.Trim());
                        if (((TextBox)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("tbxMotelCostCADEdit")).Text.Trim() != "") motelCostCad = Decimal.Parse(((TextBox)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("tbxMotelCostCADEdit")).Text.Trim());
                        totalCostCad = (lhCostCad * decimal.Parse(lHQuantity.ToString()));
                        if (mealsCostCad.HasValue && mealsQuantity.HasValue) totalCostCad = totalCostCad + (mealsCostCad.Value * decimal.Parse(mealsQuantity.Value.ToString()));
                        if (motelCostCad.HasValue && motelQuantity.HasValue) totalCostCad = totalCostCad + (motelCostCad.Value * decimal.Parse(motelQuantity.Value.ToString()));
                        totalCostCad = Decimal.Round(totalCostCad, 2);
                    }
                    else
                    {
                        lhCostUsd = Decimal.Parse(((TextBox)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("tbxLHCostUSDEdit")).Text.Trim());
                        if (((TextBox)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("tbxMealsCostUSDEdit")).Text.Trim() != "") mealsCostUsd = Decimal.Parse(((TextBox)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("tbxMealsCostUSDEdit")).Text.Trim());
                        if (((TextBox)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("tbxMotelCostUSDEdit")).Text.Trim() != "") motelCostUsd = Decimal.Parse(((TextBox)grdTeamMembers.Rows[e.RowIndex].Cells[0].FindControl("tbxMotelCostUSDEdit")).Text.Trim());
                        totalCostUsd = (lhCostUsd * decimal.Parse(lHQuantity.ToString()));
                        if (mealsCostUsd.HasValue && mealsQuantity.HasValue) totalCostUsd = totalCostUsd + (mealsCostUsd.Value * decimal.Parse(mealsQuantity.Value.ToString()));
                        if (motelCostUsd.HasValue && motelQuantity.HasValue) totalCostUsd = totalCostUsd + (motelCostUsd.Value * decimal.Parse(motelQuantity.Value.ToString()));
                        totalCostUsd = Decimal.Round(totalCostUsd, 2);
                    }

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

                    // Update data
                    ProjectCostingSheetInformationLabourHoursInformation model = new ProjectCostingSheetInformationLabourHoursInformation(projectCostingSheetInformationTDS);
                    model.Update(costingSheetId, work_, employeeId, refId, lHQuantity, unitOfMeasurementLH, unitOfMeasurementMeals, mealsQuantity, unitOfMeasurementMotel, motelQuantity, lhCostCad, mealsCostCad, motelCostCad, totalCostCad, lhCostUsd, mealsCostUsd, motelCostUsd, totalCostUsd, false, companyId, name, startDate, endDate);

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

                    StepLabourHoursInformationProcessGrid();
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                e.Cancel = true;
            }
        }
        protected void grdTeamMembers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Add":
                    // Team member Gridview, if the gridview is edition mode
                    if (grdTeamMembers.EditIndex >= 0)
                    {
                        grdTeamMembers.UpdateRow(grdTeamMembers.EditIndex, true);
                    }

                    // Add New Team Member
                    int projectId = Int32.Parse(hdfProjectId.Value);
                    ProjectGateway projectGateway = new ProjectGateway();
                    projectGateway.LoadByProjectId(projectId);

                    if (projectGateway.GetCountryID(projectId) == 1) //Canada
                    {
                        Page.Validate("labourHoursCadNew");
                    }
                    else
                    {
                        Page.Validate("labourHoursUsdNew");
                    }
                    if (Page.IsValid)
                    {
                        Page.Validate("labourHoursNew");
                        if (Page.IsValid)
                        {
                            int companyId = Int32.Parse(hdfCompanyId.Value);
                            string typeOfWork = ((DropDownList)grdTeamMembers.FooterRow.FindControl("ddlTypeOfWork_New")).SelectedValue;
                            int employeeId = Int32.Parse(((DropDownList)grdTeamMembers.FooterRow.FindControl("ddlTeamMemberNew")).SelectedValue);
                            string name = ((DropDownList)grdTeamMembers.FooterRow.FindControl("ddlTeamMemberNew")).SelectedItem.Text;
                            string unitOfMeasurementLH = ((DropDownList)grdTeamMembers.FooterRow.FindControl("ddlUnitOfMeasurementLHNew")).SelectedValue;
                            double lHQuantity = double.Parse(((TextBox)grdTeamMembers.FooterRow.FindControl("tbxLHQtyNew")).Text.Trim());
                            string unitOfMeasurementMeals = ((DropDownList)grdTeamMembers.FooterRow.FindControl("ddlUnitsOfMeasurementLHMealsNew")).SelectedValue;
                            int? mealsQuantity = null; if (((TextBox)grdTeamMembers.FooterRow.FindControl("tbxMealsQtyNew")).Text.Trim() != "") mealsQuantity = Int32.Parse(((TextBox)grdTeamMembers.FooterRow.FindControl("tbxMealsQtyNew")).Text.Trim());
                            string unitOfMeasurementMotel = ((DropDownList)grdTeamMembers.FooterRow.FindControl("ddlUnitsOfMeasurementLHMotelNew")).SelectedValue;
                            int? motelQuantity = null; if (((TextBox)grdTeamMembers.FooterRow.FindControl("tbxMotelQtyNew")).Text.Trim() != "") motelQuantity = Int32.Parse(((TextBox)grdTeamMembers.FooterRow.FindControl("tbxMotelQtyNew")).Text.Trim());

                            decimal lhCostCad = 0;
                            decimal? mealsCostCad = null;
                            decimal? motelCostCad = null;
                            decimal totalCostCad = 0;
                            decimal lhCostUsd = 0;
                            decimal? mealsCostUsd = null;
                            decimal? motelCostUsd = null;
                            decimal totalCostUsd = 0;
                            if (projectGateway.GetCountryID(projectId) == 1) //Canada
                            {
                                lhCostCad = Decimal.Parse(((TextBox)grdTeamMembers.FooterRow.FindControl("tbxLHCostCADNew")).Text.Trim());
                                if (((TextBox)grdTeamMembers.FooterRow.FindControl("tbxMealsCostCADNew")).Text.Trim() != "") mealsCostCad = Decimal.Parse(((TextBox)grdTeamMembers.FooterRow.FindControl("tbxMealsCostCADNew")).Text.Trim());
                                if (((TextBox)grdTeamMembers.FooterRow.FindControl("tbxMotelCostCADNew")).Text.Trim() != "") motelCostCad = Decimal.Parse(((TextBox)grdTeamMembers.FooterRow.FindControl("tbxMotelCostCADNew")).Text.Trim());
                                totalCostCad = (lhCostCad * decimal.Parse(lHQuantity.ToString()));
                                if (mealsCostCad.HasValue && mealsQuantity.HasValue) totalCostCad = totalCostCad + (mealsCostCad.Value * decimal.Parse(mealsQuantity.Value.ToString()));
                                if (motelCostCad.HasValue && motelQuantity.HasValue) totalCostCad = totalCostCad + (motelCostCad.Value * decimal.Parse(motelQuantity.Value.ToString()));
                                totalCostCad = Decimal.Round(totalCostCad, 2);
                            }
                            else
                            {
                                lhCostUsd = Decimal.Parse(((TextBox)grdTeamMembers.FooterRow.FindControl("tbxLHCostUSDNew")).Text.Trim());
                                if (((TextBox)grdTeamMembers.FooterRow.FindControl("tbxMealsCostUSDNew")).Text.Trim() != "") mealsCostUsd = Decimal.Parse(((TextBox)grdTeamMembers.FooterRow.FindControl("tbxMealsCostUSDNew")).Text.Trim());
                                if (((TextBox)grdTeamMembers.FooterRow.FindControl("tbxMotelCostUSDNew")).Text.Trim() != "") motelCostUsd = Decimal.Parse(((TextBox)grdTeamMembers.FooterRow.FindControl("tbxMotelCostUSDNew")).Text.Trim());
                                totalCostUsd = (lhCostUsd * decimal.Parse(lHQuantity.ToString()));
                                if (mealsCostUsd.HasValue && mealsQuantity.HasValue) totalCostUsd = totalCostUsd + (mealsCostUsd.Value * decimal.Parse(mealsQuantity.Value.ToString()));
                                if (motelCostUsd.HasValue && motelQuantity.HasValue) totalCostUsd = totalCostUsd + (motelCostUsd.Value * decimal.Parse(motelQuantity.Value.ToString()));
                                totalCostUsd = Decimal.Round(totalCostUsd, 2);
                            }

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

                            ProjectCostingSheetInformationLabourHoursInformation model = new ProjectCostingSheetInformationLabourHoursInformation(projectCostingSheetInformationTDS);
                            model.Insert(0, typeOfWork, employeeId, lHQuantity, unitOfMeasurementLH, unitOfMeasurementMeals, mealsQuantity, unitOfMeasurementMotel, motelQuantity, lhCostCad, mealsCostCad, motelCostCad, totalCostCad, lhCostUsd, mealsCostUsd, motelCostUsd, totalCostUsd, false, companyId, name, startDate, endDate);

                            Session.Remove("labourHoursInformationDummy");
                            labourHoursInformation = (ProjectCostingSheetInformationTDS.LabourHoursInformationDataTable)model.Table;
                            Session["labourHoursInformation"] = labourHoursInformation;
                            Session["projectCostingSheetInformationTDS"] = projectCostingSheetInformationTDS;

                            grdTeamMembers.DataBind();

                            StepLabourHoursInformationProcessGrid();
                        }
                    }
                    break;
            }
        }