// ////////////////////////////////////////////////////////////////////////
        // 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["material_state"] == null) || ((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_state.aspx");
                }

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

                // 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_summary.aspx
                if ((string)Request.QueryString["source_page"] == "materials_summary.aspx")
                {
                    // Store Navigator State and Update control
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];

                    // Get project record
                    materialsInformationTDS = (MaterialsInformationTDS)Session["materialsInformationTDS"];
                    MaterialsInformationBasicInformationGateway materialsInformationBasicInformationGateway = new MaterialsInformationBasicInformationGateway(materialsInformationTDS);
                    materialsInformationBasicInformationGateway.LoadByMaterialId(materialId, companyId);

                    // Store datasets
                    Session["materialsInformationTDS"] = materialsInformationTDS;
                }
            }
            else
            {
                // Restore dataset
                materialsInformationTDS = (MaterialsInformationTDS)Session["materialsInformationTDS"];
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // 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;
            }
        }
 /// <summary>
 /// Update material (direct to DB)
 /// </summary>
 /// <param name="originalMaterialId">originalMaterialId</param>       
 /// <param name="originalDescription">originalDescription</param>
 /// <param name="originalSize">originalSize</param>
 /// <param name="originalLength">originalLength</param>
 /// <param name="originalThickness">originalThickness</param>
 /// <param name="originalType">originalType</param>
 /// <param name="originalState">originalState</param>
 /// <param name="originalDeleted">originalDeleted</param>        
 /// <param name="originalCompanyId">originalCompanyId</param>
 ///
 /// <param name="newDescription">newDescription</param>
 /// <param name="newSize">newSize</param>
 /// <param name="newLength">newLength</param>
 /// <param name="newThickness">newThickness</param>
 /// <param name="newType">newType</param>
 /// <param name="newState">newState</param>
 /// <param name="newDeleted">newDeleted</param>        
 /// <param name="newCompanyId">newCompanyId</param>
 public void UpdateDirect(int originalMaterialId, string originalDescription, string originalSize, string originalLength, string originalThickness, string originalType, string originalState, bool originalDeleted, int originalCompanyId, int newMaterialId, string newDescription, string newSize, string newLength, string newThickness, string newType, string newState, bool newDeleted, int newCompanyId)
 {
     MaterialsGateway materialsGateway = new MaterialsGateway(null);
     materialsGateway.Update(originalMaterialId, originalDescription, originalSize, originalLength, originalThickness, originalType, originalState, originalDeleted, originalCompanyId, newMaterialId, newDescription, newSize, newLength, newThickness, newType, newState, newDeleted, newCompanyId);
 }
 // ////////////////////////////////////////////////////////////////////////
 // PUBLIC METHODS
 //
 /// <summary>
 /// Insert a new material (direct to DB)
 /// </summary>
 /// <param name="materialId">materialId</param>
 /// <param name="description">description</param>
 /// <param name="size">size</param>
 /// <param name="length">length</param>
 /// <param name="thickness">thickness</param>
 /// <param name="type">type</param>
 /// <param name="state">state</param>
 /// <param name="deleted">deleted</param>
 /// <param name="companyId">companyId</param>
 public void InsertDirect(int materialId, string description, string size, string length, string thickness, string type, string state, bool deleted, int companyId)
 {
     MaterialsGateway materialsGateway = new MaterialsGateway(null);
     materialsGateway.Insert(materialId, description, size, length, thickness, type, state, deleted, companyId);
 }
 /// <summary>
 /// DeleteDirect
 /// </summary>
 /// <param name="originalMaterialId">originalMaterialId</param>
 public void DeleteDirect(int originalMaterialId)
 {
     MaterialsGateway materialsGateway = new MaterialsGateway(null);
     materialsGateway.Delete(originalMaterialId);
 }
        /// <summary>
        /// GetCostUsd
        /// </summary>
        /// <param name="startDate">startDate</param>
        /// <param name="endDate">endDate</param>
        /// <param name="description">description</param>
        /// <param name="work_">work_</param>
        /// <param name="companyId">companyId</param>
        /// <returns>cost</returns>
        private decimal GetCostUsd(DateTime startDate, DateTime endDate, string description, string work_, int companyId)
        {
            decimal cost = 0;
            MaterialsGateway materialsGateway = new MaterialsGateway();
            materialsGateway.LoadByDescription(description, companyId);

            if (materialsGateway.Table.Rows.Count > 0)
            {
                int materialId = materialsGateway.GetMaterialId(description);

                ProjectCostingSheetAddMaterialListGateway projectCostingSheetAddMaterialListGateway = new ProjectCostingSheetAddMaterialListGateway();
                projectCostingSheetAddMaterialListGateway.LoadByStartDateEndDateMaterialIdWork_(startDate, endDate, materialId, work_);

                if (projectCostingSheetAddMaterialListGateway.Table.Rows.Count <= 0)
                {
                    projectCostingSheetAddMaterialListGateway.LoadByStartDateEndDateMaterialId(startDate, endDate, materialId);
                    if (projectCostingSheetAddMaterialListGateway.Table.Rows.Count <= 0)
                    {
                        projectCostingSheetAddMaterialListGateway.LoadByStartDateMaterialIdWork_(startDate, materialId, work_);
                        if (projectCostingSheetAddMaterialListGateway.Table.Rows.Count <= 0)
                        {
                            projectCostingSheetAddMaterialListGateway.LoadByStartDateMaterialId(startDate, materialId);
                            if (projectCostingSheetAddMaterialListGateway.Table.Rows.Count <= 0)
                            {
                                projectCostingSheetAddMaterialListGateway.LoadByMaterialId(materialId);
                            }
                        }
                    }
                }

                DataRow materialRow = projectCostingSheetAddMaterialListGateway.GetRow(materialId);
                cost = (decimal)materialRow["CostUsd"];
            }

            return cost;
        }