protected void grdOtherCosts_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Add":
                    // Other costs Gridview, if the gridview is edition mode
                    if (grdOtherCosts.EditIndex >= 0)
                    {
                        grdOtherCosts.UpdateRow(grdOtherCosts.EditIndex, true);
                    }

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

                    if (projectGateway.GetCountryID(projectId) == 1) //Canada
                    {
                        Page.Validate("otherCostsCadNew");
                    }
                    else
                    {
                        Page.Validate("otherCostsUsdNew");
                    }
                    if (Page.IsValid)
                    {
                        Page.Validate("otherCostsNew");
                        if (Page.IsValid)
                        {
                            int companyId = Int32.Parse(hdfCompanyId.Value);
                            string description = ((TextBox)grdOtherCosts.FooterRow.FindControl("tbxDescriptionNew")).Text.Trim();
                            string unitOfMeasurement = ((DropDownList)grdOtherCosts.FooterRow.FindControl("ddlUnitOfMeasurementOthersNew")).SelectedValue;
                            double quantity = double.Parse(((TextBox)grdOtherCosts.FooterRow.FindControl("tbxQuantityNew")).Text.Trim());
                            decimal costCad = 0;
                            decimal totalCostCad = 0;
                            decimal costUsd = 0;
                            decimal totalCostUsd = 0;
                            if (projectGateway.GetCountryID(projectId) == 1) //Canada
                            {
                                costCad = Decimal.Parse(((TextBox)grdOtherCosts.FooterRow.FindControl("tbxCostCADNew")).Text.Trim());
                                totalCostCad = (costCad * decimal.Parse(quantity.ToString()));
                                totalCostCad = Decimal.Round(totalCostCad, 2);
                            }
                            else
                            {
                                costUsd = Decimal.Parse(((TextBox)grdOtherCosts.FooterRow.FindControl("tbxCostUSDNew")).Text.Trim());
                                totalCostUsd = (costUsd * decimal.Parse(quantity.ToString()));
                                totalCostUsd = Decimal.Round(totalCostUsd, 2);
                            }

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

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

                            ProjectCostingSheetInformationOtherCostsInformation model = new ProjectCostingSheetInformationOtherCostsInformation(projectCostingSheetInformationTDS);
                            model.Insert(0, work_, function_, description, unitOfMeasurement, quantity, costCad, totalCostCad, costUsd, totalCostUsd, false, companyId, workFunctionConcat, startDate, endDate);

                            Session.Remove("otherCostsInformationDummy");
                            otherCostsInformation = (ProjectCostingSheetInformationTDS.OtherCostsInformationDataTable)model.Table;
                            Session["otherCostsInformation"] = otherCostsInformation;
                            Session["projectCostingSheetInformationTDS"] = projectCostingSheetInformationTDS;

                            grdOtherCosts.DataBind();

                            StepOtherCostsInformationProcessGrid();
                        }
                    }
                    break;
            }
        }
        protected void grdOtherCosts_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("otherCostsCadEdit");
            }
            else
            {
                Page.Validate("otherCostsUsdEdit");
            }
            if (Page.IsValid)
            {
                Page.Validate("otherCostsEdit");
                if (Page.IsValid)
                {
                    int costingSheetId = (int)e.Keys["CostingSheetID"];
                    int refId = (int)e.Keys["RefID"];
                    int companyId = Int32.Parse(hdfCompanyId.Value);
                    string description = ((TextBox)grdOtherCosts.Rows[e.RowIndex].Cells[0].FindControl("tbxDescriptionEdit")).Text.Trim();
                    string unitOfMeasurement = ((DropDownList)grdOtherCosts.Rows[e.RowIndex].Cells[0].FindControl("ddlUnitOfMeasurementOthersEdit")).SelectedValue;
                    double quantity = double.Parse(((TextBox)grdOtherCosts.Rows[e.RowIndex].Cells[0].FindControl("tbxQuantityEdit")).Text.Trim());
                    decimal costCad = 0;
                    decimal totalCostCad = 0;
                    decimal costUsd = 0;
                    decimal totalCostUsd = 0;
                    if (projectGateway.GetCountryID(projectId) == 1) //Canada
                    {
                        costCad = Decimal.Parse(((TextBox)grdOtherCosts.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)grdOtherCosts.Rows[e.RowIndex].Cells[0].FindControl("tbxCostUSDEdit")).Text.Trim());
                        totalCostUsd = (costUsd * decimal.Parse(quantity.ToString()));
                        totalCostUsd = Decimal.Round(totalCostUsd, 2);
                    }

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

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

                    // Update data
                    ProjectCostingSheetInformationOtherCostsInformation model = new ProjectCostingSheetInformationOtherCostsInformation(projectCostingSheetInformationTDS);
                    model.Update(costingSheetId, refId, work_, function_, description, unitOfMeasurement, quantity, costCad, totalCostCad, costUsd, totalCostUsd, false, companyId, workFunctionConcat, startDate, endDate);

                    otherCostsInformation = (ProjectCostingSheetInformationTDS.OtherCostsInformationDataTable)model.Table;
                    Session["otherCostsInformation"] = otherCostsInformation;
                    Session["projectCostingSheetInformationTDS"] = projectCostingSheetInformationTDS;

                    StepOtherCostsInformationProcessGrid();
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                e.Cancel = true;
            }
        }
        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 grdOtherCosts_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Other costs Gridview, if the gridview is edition mode
            if (grdOtherCosts.EditIndex >= 0)
            {
                grdOtherCosts.UpdateRow(grdOtherCosts.EditIndex, true);
            }

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

            ProjectCostingSheetInformationOtherCostsInformation model = new ProjectCostingSheetInformationOtherCostsInformation(projectCostingSheetInformationTDS);

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

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

            StepOtherCostsInformationProcessGrid();
        }