/// <summary>
        /// Delete
        /// </summary>
        /// <param name="costId">costId</param>
        /// <param name="materialId">materialId</param>
        public void Delete(int costId, int materialId)
        {
            MaterialsInformationTDS.CostHistoryInformationRow row = GetRow(costId, materialId);
            row.Deleted = true;

            MaterialsInformationCostHistoryExceptionsInformation model = new MaterialsInformationCostHistoryExceptionsInformation(Data);
            model.DeleteAll(costId);
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_RESOURCES_MATERIALS_VIEW"]) && Convert.ToBoolean(Session["sgLFS_RESOURCES_MATERIALS_EDIT"])))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                }

                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) || ((string)Request.QueryString["material_id"] == null) || ((string)Request.QueryString["active_tab"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in materials_edit.aspx");
                }

                // Tag Page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfResourceType.Value = "Materials";
                hdfActiveTab.Value = Request.QueryString["active_tab"].ToString();
                hdfLoginId.Value = Convert.ToInt32(Session["loginID"]).ToString();
                hdfCurrentMaterialId.Value = Request.QueryString["material_id"];

                // Prepare initial data
                Session.Remove("materialsNotesDummy");
                Session.Remove("materialsCostsDummy");
                Session.Remove("materialsCostsExceptionsDummy");
                Session.Remove("costIdSelected");

                // ... for team member title
                int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
                int materialId = Int32.Parse(hdfCurrentMaterialId.Value);

                MaterialsGateway materialsGateway = new MaterialsGateway();
                materialsGateway.LoadByMaterialId(materialId, companyId);
                lblTitleMaterials.Text = "Material: " + materialsGateway.GetDescription(materialId);

                // If coming from
                string resourceType = hdfResourceType.Value;

                // ... materials_navigator2.aspx or materials_add.aspx
                if ((Request.QueryString["source_page"] == "materials_navigator2.aspx")|| (Request.QueryString["source_page"] == "materials_add.aspx"))
                {
                    StoreNavigatorState();
                    ViewState["update"] = "no";

                    // ... Set initial tab
                    if ((string)Session["dialogOpenedMaterials"] != "1")
                    {
                        hdfActiveTab.Value = Request.QueryString["active_tab"];

                        materialsInformationTDS = new MaterialsInformationTDS();
                        materialsTDS = new MaterialsTDS();

                        MaterialsInformationBasicInformation materialsInformationBasicInformation = new MaterialsInformationBasicInformation(materialsInformationTDS);
                        materialsInformationBasicInformation.LoadByMaterialId(materialId, companyId);

                        MaterialsInformationCostHistoryInformation materialsInformationCostHistoryInformation = new MaterialsInformationCostHistoryInformation(materialsInformationTDS);
                        materialsInformationCostHistoryInformation.LoadAllByMaterialId(materialId, companyId);

                        MaterialsInformationCostHistoryExceptionsInformation materialsInformationCostHistoryExceptionsInformation = new MaterialsInformationCostHistoryExceptionsInformation(materialsInformationTDS);
                        materialsInformationCostHistoryExceptionsInformation.LoadAllByMaterialId(materialId, companyId);

                        MaterialsInformationNoteInformation materialsInformationNoteInformationForEdit = new MaterialsInformationNoteInformation(materialsInformationTDS);
                        materialsInformationNoteInformationForEdit.LoadAllByMaterialId(materialId, companyId);
                    }
                    else
                    {
                        hdfActiveTab.Value = (string)Session["activeTabMaterials"];

                        // Restore datasets
                        materialsTDS = (MaterialsTDS)Session["materialsTDS"];
                        materialsInformationTDS = (MaterialsInformationTDS)Session["materialsInformationTDS"];
                    }

                    // ... Store dataset
                    Session["materialsInformationTDS"] = materialsInformationTDS;
                    Session["materialsTDS"] = materialsTDS;
                }

                // ... materials_summary.aspx or materials_edit
                if ((Request.QueryString["source_page"] == "materials_summary.aspx") || (Request.QueryString["source_page"] == "materials_edit.aspx"))
                {
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];

                    // ... Restore dataset
                    materialsTDS = (MaterialsTDS)Session["materialsTDS"];
                    materialsInformationTDS = (MaterialsInformationTDS)Session["materialsInformationTDS"];

                    // ... Set initial tab
                    if ((string)Session["dialogOpenedMaterials"] != "1")
                    {
                        hdfActiveTab.Value = Request.QueryString["active_tab"];
                    }
                    else
                    {
                        hdfActiveTab.Value = (string)Session["activeTabMaterials"];
                    }

                    if (ViewState["update"].ToString().Trim() == "yes")
                    {
                        MaterialsInformationBasicInformation materialsInformationBasicInformation = new MaterialsInformationBasicInformation(materialsInformationTDS);
                        materialsInformationBasicInformation.LoadByMaterialId(materialId, companyId);

                        MaterialsInformationCostHistoryInformation materialsInformationCostHistoryInformation = new MaterialsInformationCostHistoryInformation(materialsInformationTDS);
                        materialsInformationCostHistoryInformation.LoadAllByMaterialId(materialId, companyId);

                        MaterialsInformationCostHistoryExceptionsInformation materialsInformationCostHistoryExceptionsInformation = new MaterialsInformationCostHistoryExceptionsInformation(materialsInformationTDS);
                        materialsInformationCostHistoryExceptionsInformation.LoadAllByMaterialId(materialId, companyId);

                        MaterialsInformationNoteInformation materialsInformationNoteInformationForEdit = new MaterialsInformationNoteInformation(materialsInformationTDS);
                        materialsInformationNoteInformationForEdit.LoadAllByMaterialId(materialId, companyId);

                        // ... Store dataset
                        Session["materialsInformationTDS"] = materialsInformationTDS;
                        Session["materialsTDS"] = materialsTDS;
                    }
                }

                Session["costIdSelected"] = 0;
                grdCostsExceptions.ShowFooter = false;
                string filterOptions = string.Format("CostID = {0} AND Deleted = {1}", 0, 0);
                odsCostsExceptions.FilterExpression = filterOptions;

                // Prepare initial data
                // ... Set initial tab
                int activeTabMaterials = Int32.Parse(hdfActiveTab.Value);
                tcDetailedInformation.ActiveTabIndex = activeTabMaterials;

                // ... Data for current material
                LoadData(companyId);

                // Databind
                Page.DataBind();
            }
            else
            {
                // Restore datasets
                materialsTDS = (MaterialsTDS)Session["materialsTDS"];
                materialsInformationTDS = (MaterialsInformationTDS)Session["materialsInformationTDS"];

                // Set initial tab
                int activeTabMaterials = Int32.Parse(hdfActiveTab.Value);
                tcDetailedInformation.ActiveTabIndex = activeTabMaterials;
            }
        }
        protected void grdCostsExceptions_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Page.Validate("EditCostException");
            if (Page.IsValid)
            {
                int costId = Int32.Parse(((Label)grdCostsExceptions.Rows[e.RowIndex].Cells[3].FindControl("lblExceptionCostIDEdit")).Text.Trim());
                int refId = Int32.Parse(((Label)grdCostsExceptions.Rows[e.RowIndex].Cells[3].FindControl("lblExceptionRefIDEdit")).Text.Trim());
                int materialId = Int32.Parse(hdfCurrentMaterialId.Value);

                string unitOfMeasurement = ((DropDownList)grdCostsExceptions.Rows[e.RowIndex].Cells[0].FindControl("ddlExceptionUnitOfMeasurementMaterialsEdit")).SelectedValue;
                decimal costCad = Decimal.Parse(((TextBox)grdCostsExceptions.Rows[e.RowIndex].Cells[3].FindControl("tbxExceptionCostCadEdit")).Text.Trim());
                decimal costUsd = Decimal.Parse(((TextBox)grdCostsExceptions.Rows[e.RowIndex].Cells[3].FindControl("tbxExceptionCostUsdEdit")).Text.Trim());
                string workFunctionConcat = ((DropDownList)grdCostsExceptions.Rows[e.RowIndex].Cells[0].FindControl("ddlExceptionWorkFunctionEdit")).SelectedValue;

                string[] workFunction = workFunctionConcat.ToString().Trim().Split('.');
                string work_ = workFunction[0].Trim();
                string function_ = workFunction[1].Trim();

                // Update data
                MaterialsInformationCostHistoryExceptionsInformation model = new MaterialsInformationCostHistoryExceptionsInformation(materialsInformationTDS);
                model.Update(costId, refId, work_, function_, unitOfMeasurement, costCad, costUsd, workFunctionConcat);

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

                string filterOptions = string.Format("CostID = {0} AND Deleted = {1}", costId, 0);
                odsCostsExceptions.FilterExpression = filterOptions;
            }
            else
            {
                e.Cancel = true;
            }
        }
        protected void grdCostsExceptions_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Costs Exceptions Gridview, if the gridview is edition mode
            if (grdCostsExceptions.EditIndex >= 0)
            {
                grdCostsExceptions.UpdateRow(grdCostsExceptions.EditIndex, true);
            }

            // Delete Cost Exceptions
            int costId = (int)e.Keys["CostID"];
            int refId = (int)e.Keys["RefID"];
            MaterialsInformationCostHistoryExceptionsInformation model = new MaterialsInformationCostHistoryExceptionsInformation(materialsInformationTDS);

            // Delete cost exception
            model.Delete(costId, refId);

            grdCostsExceptions.DataBind();
            grdCosts.DataBind();

            // Store dataset
            Session["materialsInformationTDS"] = materialsInformationTDS;
        }
        private void UpdateDatabase()
        {
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int materialId = Int32.Parse(hdfCurrentMaterialId.Value);

            DB.Open();
            DB.BeginTransaction();
            try
            {
                // Save notes information
                MaterialsInformationNoteInformation materialsInformationNoteInformation = new MaterialsInformationNoteInformation(materialsInformationTDS);
                materialsInformationNoteInformation.Save(companyId);

                // Save costs information
                MaterialsInformationCostHistoryInformation materialsInformationCostHistoryInformation = new MaterialsInformationCostHistoryInformation(materialsInformationTDS);
                materialsInformationCostHistoryInformation.Save(companyId);

                // Save costs exceptions information
                MaterialsInformationCostHistoryExceptionsInformation materialsInformationCostHistoryExceptionsInformation = new MaterialsInformationCostHistoryExceptionsInformation(materialsInformationTDS);
                materialsInformationCostHistoryExceptionsInformation.Save(companyId, materialId);

                // Save material information
                MaterialsInformationBasicInformation materialsInformationBasicInformation = new MaterialsInformationBasicInformation(materialsInformationTDS);
                materialsInformationBasicInformation.Save(companyId);

                DB.CommitTransaction();

                // Store datasets
                materialsInformationTDS.AcceptChanges();
                Session["materialsInformationTDS"] = materialsInformationTDS;
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        private void GrdCostsExceptionsAdd()
        {
            if (ValidateCostsExceptionsFooter())
            {
                Page.Validate("AddCostException");
                if (Page.IsValid)
                {
                    int costId = Convert.ToInt32(Session["costIdSelected"]);
                    int companyId = Int32.Parse(hdfCompanyId.Value);

                    string unitOfMeasurement = ((DropDownList)grdCostsExceptions.FooterRow.FindControl("ddlExceptionUnitOfMeasurementMaterialsNew")).SelectedValue;
                    decimal costCad = Decimal.Parse(((TextBox)grdCostsExceptions.FooterRow.FindControl("tbxExceptionCostCadNew")).Text.Trim());
                    decimal costUsd = Decimal.Parse(((TextBox)grdCostsExceptions.FooterRow.FindControl("tbxExceptionCostUsdNew")).Text.Trim());
                    string workFunctionConcat = ((DropDownList)grdCostsExceptions.FooterRow.FindControl("ddlExceptionWorkFunctionNew")).SelectedValue;

                    string[] workFunction = workFunctionConcat.ToString().Trim().Split('.');
                    string work_ = workFunction[0].Trim();
                    string function_ = workFunction[1].Trim();

                    MaterialsInformationCostHistoryExceptionsInformation model = new MaterialsInformationCostHistoryExceptionsInformation(materialsInformationTDS);
                    model.Insert(costId, work_, function_, unitOfMeasurement, costCad, costUsd, false, companyId, false, workFunctionConcat);

                    Session.Remove("materialsCostsExceptionsDummy");
                    Session["materialsInformationTDS"] = materialsInformationTDS;

                    string filterOptions = string.Format("CostID = {0} AND Deleted = {1}", costId, 0);
                    odsCostsExceptions.FilterExpression = filterOptions;

                    grdCostsExceptions.DataBind();
                }
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_RESOURCES_EMPLOYEES_VIEW"]) && Convert.ToBoolean(Session["sgLFS_RESOURCES_EMPLOYEES_DELETE"])))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                }

                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) || ((string)Request.QueryString["material_id"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in materials_delete.aspx");
                }

                // Tag page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfMaterialId.Value = Request.QueryString["material_id"];
                hdfActiveTab.Value = Request.QueryString["active_tab"];

                // Prepare initial data
                // ... for material title
                int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
                int materialId = Int32.Parse(hdfMaterialId.Value);

                MaterialsGateway materialsGateway = new MaterialsGateway();
                materialsGateway.LoadByMaterialId(materialId, companyId);
                lblTitleMaterial.Text = "Material: " + materialsGateway.GetDescription(materialId);

                // If coming from
                // ... materials_navigator2.aspx
                if (Request.QueryString["source_page"] == "materials_navigator2.aspx")
                {
                    StoreNavigatorState();
                    ViewState["update"] = "no";

                    materialsInformationTDS = new MaterialsInformationTDS();
                    materialsTDS = new MaterialsTDS();

                    MaterialsInformationBasicInformation materialsInformationBasicInformation = new MaterialsInformationBasicInformation(materialsInformationTDS);
                    materialsInformationBasicInformation.LoadByMaterialId(materialId, companyId);

                    MaterialsInformationCostHistoryInformation materialsInformationCostHistoryInformation = new MaterialsInformationCostHistoryInformation(materialsInformationTDS);
                    materialsInformationCostHistoryInformation.LoadAllByMaterialId(materialId, companyId);

                    MaterialsInformationCostHistoryExceptionsInformation materialsInformationCostHistoryExceptionsInformation = new MaterialsInformationCostHistoryExceptionsInformation(materialsInformationTDS);
                    materialsInformationCostHistoryExceptionsInformation.LoadAllByMaterialId(materialId, companyId);

                    MaterialsInformationNoteInformation materialsInformationNoteInformation = new MaterialsInformationNoteInformation(materialsInformationTDS);
                    materialsInformationNoteInformation.LoadAllByMaterialId(materialId, companyId);

                    // Store dataset
                    Session["materialsInformationTDS"] = materialsInformationTDS;
                    Session["materialsTDS"] = materialsTDS;
                }

                // ... materials_summary.aspx
                if (Request.QueryString["source_page"] == "materials_summary.aspx")
                {
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];

                    // Restore dataset
                    materialsTDS = (MaterialsTDS)Session["materialsTDS"];
                    materialsInformationTDS = (MaterialsInformationTDS)Session["materialsInformationTDS"];
                }
            }
            else
            {
                // Restore datasets
                materialsTDS = (MaterialsTDS)Session["materialsTDS"];
                materialsInformationTDS = (MaterialsInformationTDS)Session["materialsInformationTDS"];
            }
        }