private void Save()
        {
            // Validate data
            bool validData = true;

            // Validate general data
            Page.Validate("general");

            // For valid data
            if (validData)
            {
                // Team Members Gridview, if the gridview is edition mode
                if (grdTeamMembers.EditIndex >= 0)
                {
                    grdTeamMembers.UpdateRow(grdTeamMembers.EditIndex, true);
                }

                // Units Gridview, if the gridview is edition mode
                if (grdUnits.EditIndex >= 0)
                {
                    grdUnits.UpdateRow(grdUnits.EditIndex, true);
                }

                // Materials Gridview, if the gridview is edition mode
                if (grdMaterials.EditIndex >= 0)
                {
                    grdMaterials.UpdateRow(grdMaterials.EditIndex, true);
                }

                // Other Costs Gridview, if the gridview is edition mode
                if (grdOtherCosts.EditIndex >= 0)
                {
                    grdOtherCosts.UpdateRow(grdOtherCosts.EditIndex, true);
                }

                // Subcontractors Costs Gridview, if the gridview is edition mode
                if (grdSubcontractors.EditIndex >= 0)
                {
                    grdSubcontractors.UpdateRow(grdSubcontractors.EditIndex, true);
                }

                // Hotel Costs Gridview, if the gridview is edition mode
                if (grdHotels.EditIndex >= 0)
                {
                    grdHotels.UpdateRow(grdHotels.EditIndex, true);
                }

                // Bondings Costs Gridview, if the gridview is edition mode
                if (grdBondings.EditIndex >= 0)
                {
                    grdBondings.UpdateRow(grdBondings.EditIndex, true);
                }

                // Insurances Costs Gridview, if the gridview is edition mode
                if (grdInsurances.EditIndex >= 0)
                {
                    grdInsurances.UpdateRow(grdInsurances.EditIndex, true);
                }

                // Other Category Costs Gridview, if the gridview is edition mode
                if (grdOtherCategory.EditIndex >= 0)
                {
                    grdOtherCategory.UpdateRow(grdOtherCategory.EditIndex, true);
                }

                // Basic
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int costingSheetId = Int32.Parse(hdfCostingSheetId.Value);

                // General
                string state = tbxState.Text;
                decimal teamMembersTotalCostCad = decimal.Parse(tbxTeamMembersTotalCostCAD.Text);
                decimal teamMembersTotalCostUsd = decimal.Parse(tbxTeamMembersTotalCostUSD.Text);
                decimal unitsTotalCostCad = decimal.Parse(tbxUnitsTotalCostsCAD.Text);
                decimal unitsTotalCostUsd = decimal.Parse(tbxUnitsTotalCostsUSD.Text);
                decimal materialsTotalCostCad = decimal.Parse(tbxMaterialsTotalCostsCAD.Text);
                decimal materialsTotalCostUsd = decimal.Parse(tbxMaterialsTotalCostsUSD.Text);
                decimal otherCostsTotalCostCad = decimal.Parse(tbxOtherCostsTotalCostsCAD.Text);
                decimal otherCostsTotalCostUsd = decimal.Parse(tbxOtherCostsTotalCostsUSD.Text);
                decimal grandTotalCostCad = decimal.Parse(tbxTotalCostCad.Text);
                decimal grandTotalCostUsd = decimal.Parse(tbxTotalCostUsd.Text);
                decimal subcontractorsTotalCostCad = decimal.Parse(tbxSubcontractorsTotalCostsCAD.Text);
                decimal subcontractorsTotalCostUsd = decimal.Parse(tbxSubcontractorsTotalCostsUSD.Text);

                decimal grandRevenue = 0.0M; if (tbxGrandRevenue.Text != "") grandRevenue = decimal.Parse(tbxGrandRevenue.Text);
                decimal grandProfit = 0.0M; if (tbxGrandProfit.Text != "") grandProfit = decimal.Parse(tbxGrandProfit.Text);
                decimal grandGrossMargin = 0.0M; if (tbxGrandGrossMargin.Text != "") { tbxGrandGrossMargin.Text = tbxGrandGrossMargin.Text.Replace('%', ' '); grandGrossMargin = decimal.Parse(tbxGrandGrossMargin.Text.Trim()); }

                // Update unit data
                ProjectCostingSheetInformationBasicInformation projectCostingSheetInformationBasicInformation = new ProjectCostingSheetInformationBasicInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationBasicInformation.Update(costingSheetId, hdfCostingSheetName.Value, teamMembersTotalCostCad, teamMembersTotalCostUsd, materialsTotalCostCad, materialsTotalCostUsd, unitsTotalCostCad, unitsTotalCostUsd, otherCostsTotalCostCad, otherCostsTotalCostUsd, grandTotalCostCad, grandTotalCostUsd, subcontractorsTotalCostCad, subcontractorsTotalCostUsd, grandRevenue, grandProfit, grandGrossMargin);

                // Store dataset
                Session["projectCostingSheetInformationTDS"] = projectCostingSheetInformationTDS;

                // Update database
                UpdateDatabase();

                ViewState["update"] = "yes";

                // Redirect
                string url = "";
                if (Request.QueryString["source_page"] == "project_costing_sheets_navigator.aspx" || Request.QueryString["source_page"] == "project_costing_sheets_add.aspx")
                {
                    url = "./project_costing_sheets_navigator.aspx?source_page=project_costing_sheets_edit.aspx&costing_sheet_id=" + hdfCostingSheetId.Value + "&project_id=" + hdfProjectId.Value + "&costing_sheet_state=Approved&active_tab=" + (string)ViewState["active_tab"] + GetNavigatorState() + "&origin=" + (string)ViewState["origin"] + "&update=" + (string)ViewState["update"] + "&data_changed=" + hdfDataChanged.Value + "&state=" + (string)ViewState["state"];
                }

                if (Request.QueryString["source_page"] == "project_costing_sheets_summary.aspx")
                {
                    url = "./project_costing_sheets_summary.aspx?source_page=project_costing_sheets_edit.aspx&costing_sheet_id=" + hdfCostingSheetId.Value + "&project_id=" + hdfProjectId.Value + "&costing_sheet_state=Approved&active_tab=" + (string)ViewState["active_tab"] + GetNavigatorState() + "&origin=" + (string)ViewState["origin"] + "&update=" + (string)ViewState["update"] + "&data_changed=" + hdfDataChanged.Value + "&state=" + (string)ViewState["state"];
                }

                Response.Redirect(url);
            }
        }
        private void Apply()
        {
            // Validate data
            bool validData = true;

            // Validate general data
            Page.Validate("general");

            // For valid data
            if (validData)
            {
                // Team Members Gridview, if the gridview is edition mode
                if (grdTeamMembers.EditIndex >= 0)
                {
                    grdTeamMembers.UpdateRow(grdTeamMembers.EditIndex, true);
                }

                // Units Gridview, if the gridview is edition mode
                if (grdUnits.EditIndex >= 0)
                {
                    grdUnits.UpdateRow(grdUnits.EditIndex, true);
                }

                // Materials Gridview, if the gridview is edition mode
                if (grdMaterials.EditIndex >= 0)
                {
                    grdMaterials.UpdateRow(grdMaterials.EditIndex, true);
                }

                // Other Costs Gridview, if the gridview is edition mode
                if (grdOtherCosts.EditIndex >= 0)
                {
                    grdOtherCosts.UpdateRow(grdOtherCosts.EditIndex, true);
                }

                // Subcontractors Costs Gridview, if the gridview is edition mode
                if (grdSubcontractors.EditIndex >= 0)
                {
                    grdSubcontractors.UpdateRow(grdSubcontractors.EditIndex, true);
                }

                // Hotel Costs Gridview, if the gridview is edition mode
                if (grdHotels.EditIndex >= 0)
                {
                    grdHotels.UpdateRow(grdHotels.EditIndex, true);
                }

                // Bondings Costs Gridview, if the gridview is edition mode
                if (grdBondings.EditIndex >= 0)
                {
                    grdBondings.UpdateRow(grdBondings.EditIndex, true);
                }

                // Insurances Costs Gridview, if the gridview is edition mode
                if (grdInsurances.EditIndex >= 0)
                {
                    grdInsurances.UpdateRow(grdInsurances.EditIndex, true);
                }

                // Other Category Costs Gridview, if the gridview is edition mode
                if (grdOtherCategory.EditIndex >= 0)
                {
                    grdOtherCategory.UpdateRow(grdOtherCategory.EditIndex, true);
                }

                // Basic
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int costingSheetId = Int32.Parse(hdfCostingSheetId.Value);

                // General
                string state = tbxState.Text;
                decimal teamMembersTotalCostCad = decimal.Parse(tbxTeamMembersTotalCostCAD.Text);
                decimal teamMembersTotalCostUsd = decimal.Parse(tbxTeamMembersTotalCostUSD.Text);
                decimal unitsTotalCostCad = decimal.Parse(tbxUnitsTotalCostsCAD.Text);
                decimal unitsTotalCostUsd = decimal.Parse(tbxUnitsTotalCostsUSD.Text);
                decimal materialsTotalCostCad = decimal.Parse(tbxMaterialsTotalCostsCAD.Text);
                decimal materialsTotalCostUsd = decimal.Parse(tbxMaterialsTotalCostsUSD.Text);
                decimal otherCostsTotalCostCad = decimal.Parse(tbxOtherCostsTotalCostsCAD.Text);
                decimal otherCostsTotalCostUsd = decimal.Parse(tbxOtherCostsTotalCostsUSD.Text);
                decimal grandTotalCostCad = decimal.Parse(tbxTotalCostCad.Text);
                decimal grandTotalCostUsd = decimal.Parse(tbxTotalCostUsd.Text);
                decimal subcontractorsTotalCostCad = decimal.Parse(tbxSubcontractorsTotalCostsCAD.Text);
                decimal subcontractorsTotalCostUsd = decimal.Parse(tbxSubcontractorsTotalCostsUSD.Text);

                decimal grandRevenue = 0.0M; if (tbxGrandRevenue.Text != "") grandRevenue = decimal.Parse(tbxGrandRevenue.Text);
                decimal grandProfit = 0.0M; if (tbxGrandProfit.Text != "") grandProfit = decimal.Parse(tbxGrandProfit.Text);
                decimal grandGrossMargin = 0.0M; if (tbxGrandGrossMargin.Text != "") { tbxGrandGrossMargin.Text = tbxGrandGrossMargin.Text.Replace('%', ' '); grandGrossMargin = decimal.Parse(tbxGrandGrossMargin.Text.Trim()); }

                // Update unit data
                ProjectCostingSheetInformationBasicInformation projectCostingSheetInformationBasicInformation = new ProjectCostingSheetInformationBasicInformation(projectCostingSheetInformationTDS);
                projectCostingSheetInformationBasicInformation.Update(costingSheetId, hdfCostingSheetName.Value, teamMembersTotalCostCad, teamMembersTotalCostUsd, materialsTotalCostCad, materialsTotalCostUsd, unitsTotalCostCad, unitsTotalCostUsd, otherCostsTotalCostCad, otherCostsTotalCostUsd, grandTotalCostCad, grandTotalCostUsd, subcontractorsTotalCostCad, subcontractorsTotalCostUsd, grandRevenue, grandProfit, grandGrossMargin);

                // Store dataset
                Session["projectCostingSheetInformationTDS"] = projectCostingSheetInformationTDS;

                // Update database
                UpdateDatabase();

                ViewState["update"] = "yes";
            }
        }