private void Apply()
        {
            // Validate data
            bool validData = false;

            Page.Validate("generalInformation");
            if (Page.IsValid)
            {
                validData = true;

                // ... Validate notes tab
                if (ValidateNotesFooter())
                {
                    Page.Validate("notesDataAdd");
                    if (!Page.IsValid)
                    {
                        validData = false;
                    }
                }

                Page.Validate("notesDataEdit");
                if (!Page.IsValid)
                {
                    validData = false;
                }
            }

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

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

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

                // Save data
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int materialId = Int32.Parse(hdfCurrentMaterialId.Value);

                // ... Get basic material data
                string thickness = "";
                string size = "";
                string length = "";
                string description = "";
                string state = tbxState.Text.Trim();

                if (hdfProcess.Value == "Full Length Lining")
                {
                    thickness = ddlThickness.SelectedValue;
                    size = tbxSize.Text.Trim();
                    description = "Full Length Liner " + size;
                    if (thickness != "")
                    {
                        description = description + " / T." + thickness;
                    }
                }

                if (hdfProcess.Value == "Point Repairs")
                {
                    size = ddlPrSize.SelectedValue;
                    length = ddlPrLength.SelectedValue;
                    string formatedSize = size.Remove(size.Length - 1, 1);
                    formatedSize = formatedSize.PadLeft(3, '0');
                    string formatedLength = length.Remove(length.Length - 1, 1);
                    formatedLength = formatedLength.PadLeft(3, '0');
                    description = "PR-" + formatedSize + "-" + formatedLength + "-000";
                }

                if (hdfProcess.Value == "Junction Lining")
                {
                    size = "";
                    if (rbtnMiscSupplies.Checked) description = "Lateral / Misc Supplies";
                    if (rbtnCleanoutMaterial.Checked) description = "Lateral / Cleanout Material";
                    if (rbtnBackfillCleanout.Checked) description = "Lateral / Backfill - Cleanout";
                    if (rbtnOther.Checked)
                    {
                        size = tbxJLSize.Text.Trim();
                        description = "Junction Liner " + size;
                    }
                }

                // Update service data
                MaterialsInformationBasicInformation materialsInformationBasicInformation = new MaterialsInformationBasicInformation(materialsInformationTDS);
                materialsInformationBasicInformation.Update(materialId, description, size, length, thickness, hdfProcess.Value, state, false, companyId);

                // Store datasets
                Session["materialsInformationTDS"] = materialsInformationTDS;
                Session["materialsTDS"] = materialsTDS;

                // Update database
                UpdateDatabase();

                ViewState["update"] = "yes";
            }
        }
        private void Save()
        {
            // Validate data
            bool validData = false;

            Page.Validate("generalInformation");
            if (Page.IsValid)
            {
                validData = true;

                // ... Validate notes tab
                if (ValidateNotesFooter())
                {
                    Page.Validate("notesDataAdd");
                    if (!Page.IsValid)
                    {
                        validData = false;
                    }
                }

                Page.Validate("notesDataEdit");
                if (!Page.IsValid)
                {
                    validData = false;
                }
            }

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

                // Save notes data
                GrdNotesAdd();

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

                // Save costs data
                GrdCostsAdd();

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

                // Save costs exceptions data
                GrdCostsExceptionsAdd();

                // Save data
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int materialId = Int32.Parse(hdfCurrentMaterialId.Value);

                // ... Get basic material data
                string thickness = "";
                string size = "";
                string length = "";
                string description = "";
                string state = tbxState.Text.Trim();

                if (hdfProcess.Value == "Full Length Lining")
                {
                    thickness =  ddlThickness.SelectedValue;
                    size = tbxSize.Text.Trim();
                    description = "Full Length Liner " + size;
                    if (thickness != "")
                    {
                        description = description + " / T." + thickness;
                    }
                }

                if (hdfProcess.Value == "Point Repairs")
                {
                    size = ddlPrSize.SelectedValue;
                    length = ddlPrLength.SelectedValue;
                    string formatedSize = size.Remove(size.Length - 1, 1);
                    formatedSize = formatedSize.PadLeft(3, '0');
                    string formatedLength = length.Remove(length.Length - 1, 1);
                    formatedLength = formatedLength.PadLeft(3, '0');
                    description = "PR-" + formatedSize + "-" + formatedLength + "-000";
                }

                if (hdfProcess.Value == "Junction Lining")
                {
                    size = "";
                    if (rbtnMiscSupplies.Checked) description = "Lateral / Misc Supplies";
                    if (rbtnCleanoutMaterial.Checked) description = "Lateral / Cleanout Material";
                    if (rbtnBackfillCleanout.Checked) description = "Lateral / Backfill - Cleanout";
                    if (rbtnRestorationCrowleCap.Checked) description = "Lateral / Restoration & Crowle Cap";
                    if (rbtnOther.Checked)
                    {
                        if (!tbxDescription.Text.Contains("Lateral"))
                        {
                            size = tbxJLSize.Text.Trim();
                            description = "Junction Liner " + size;
                        }
                        else
                        {
                            description = tbxDescription.Text;
                        }
                    }
                }

                // Update service data
                MaterialsInformationBasicInformation materialsInformationBasicInformation = new MaterialsInformationBasicInformation(materialsInformationTDS);
                materialsInformationBasicInformation.Update(materialId, description, size, length, thickness, hdfProcess.Value, state, false, companyId);

                // Store datasets
                Session["materialsInformationTDS"] = materialsInformationTDS;
                Session["materialsTDS"] = materialsTDS;

                // Update database
                UpdateDatabase();

                ViewState["update"] = "yes";

                // Redirect
                string url = "";
                if (Request.QueryString["source_page"] == "materials_navigator2.aspx")
                {
                    url = "./materials_navigator2.aspx?source_page=materials_edit.aspx&material_id=" + hdfCurrentMaterialId.Value + GetNavigatorState() + "&update=yes";
                }

                if (Request.QueryString["source_page"] == "materials_summary.aspx")
                {
                    string activeTabMaterials = hdfActiveTab.Value;
                    url = "./materials_summary.aspx?source_page=materials_edit.aspx&material_id=" + hdfCurrentMaterialId.Value + "&active_tab=" + activeTabMaterials + GetNavigatorState() + "&update=yes";
                }

                Response.Redirect(url);
            }
        }
        private void UpdateState()
        {
            int materialId = Int32.Parse(hdfMaterialId.Value);
            MaterialsInformationBasicInformationGateway materialsInformationBasicInformationGateway = new MaterialsInformationBasicInformationGateway(materialsInformationTDS);

            // General Data
            string description = materialsInformationBasicInformationGateway.GetDescription(materialId);
            string size = materialsInformationBasicInformationGateway.GetSize(materialId);
            string length = materialsInformationBasicInformationGateway.GetLength(materialId);
            string thickness = materialsInformationBasicInformationGateway.GetThickness(materialId);
            string type = materialsInformationBasicInformationGateway.GetType(materialId);
            int companyId = Int32.Parse(hdfCompanyId.Value);

            // ... Get new values
            string newState = null;
            switch ((string)Request.QueryString["material_state"])
            {
                case "Available":
                    newState = "Not Available";
                    break;

                case "Not Available":
                    newState = "Available";
                    break;
            }

            // Update service data
            MaterialsInformationBasicInformation materialsInformationBasicInformation = new MaterialsInformationBasicInformation(materialsInformationTDS);
            materialsInformationBasicInformation.Update(materialId, description, size, length, thickness, type, newState, false, companyId);
        }