protected void grdMaterials_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Materials Gridview, if the gridview is edition mode
            if (grdMaterials.EditIndex >= 0)
            {
                grdMaterials.UpdateRow(grdMaterials.EditIndex, true);
            }

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

            ProjectCostingSheetAddMaterialsInformation model = new ProjectCostingSheetAddMaterialsInformation(projectCostingSheetAddTDS);
            model.Delete(costingSheetId, materialId, refId);

            // Store tables
            Session.Remove("materialsInformationDummy");
            materialsInformation = (ProjectCostingSheetAddTDS.MaterialsInformationDataTable)model.Table;
            Session["materialsInformation"] = materialsInformation;
            Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

            StepMaterialInformationProcessGrid();
        }
        // /////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        // STEP6 - MATERIAL INFORMATION
        //
        // ////////////////////////////////////////////////////////////////////////
        // STEP6 - MATERIAL INFORMATION - EVENTS
        //
        protected void grdMaterials_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Add":
                    // Materials Gridview, if the gridview is edition mode
                    if (grdMaterials.EditIndex >= 0)
                    {
                        grdMaterials.UpdateRow(grdMaterials.EditIndex, true);
                    }

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

                    if (Page.IsValid)
                    {
                        // Validate costs
                        projectGateway.LoadByProjectId(projectId);

                        if (projectGateway.GetCountryID(projectId) == 1) //Canada
                        {
                            Page.Validate("materialsCadNew");
                        }
                        else
                        {
                            Page.Validate("materialsUsdNew");
                        }
                    }

                    if (Page.IsValid)
                    {
                        int companyId = Int32.Parse(hdfCompanyId.Value);
                        string description = ((DropDownList)grdMaterials.FooterRow.FindControl("ddlDescriptionNew")).SelectedItem.Text;
                        int materialId = Int32.Parse(((DropDownList)grdMaterials.FooterRow.FindControl("ddlDescriptionNew")).SelectedValue);
                        string unitOfMeasurement = ((DropDownList)grdMaterials.FooterRow.FindControl("ddlUnitOfMeasurementMaterialsNew")).SelectedValue;
                        double quantity = double.Parse(((TextBox)grdMaterials.FooterRow.FindControl("tbxQuantityNew")).Text.Trim());

                        decimal costCad = 0.0M;
                        decimal totalCostCad = 0.0M;
                        decimal costUsd = 0.0M;
                        decimal totalCostUsd = 0.0M;

                        if (projectGateway.GetCountryID(projectId) == 1) //Canada
                        {
                            costCad = Decimal.Parse(((TextBox)grdMaterials.FooterRow.FindControl("tbxCostCADNew")).Text.Trim());
                            totalCostCad = (costCad * decimal.Parse(quantity.ToString()));
                            totalCostCad = Decimal.Round(totalCostCad, 2);
                        }
                        else
                        {
                            costUsd = Decimal.Parse(((TextBox)grdMaterials.FooterRow.FindControl("tbxCostUSDNew")).Text.Trim());
                            totalCostUsd = (costUsd * decimal.Parse(quantity.ToString()));
                            totalCostUsd = Decimal.Round(totalCostUsd, 2);
                        }

                        LFSLive.DA.Resources.Materials.MaterialsGateway m = new LiquiForce.LFSLive.DA.Resources.Materials.MaterialsGateway();
                        m.LoadByMaterialId(materialId, companyId);
                        string process = m.GetType(materialId);

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

                        string workFunctionConcat = "Full Length . Install";
                        string function_ = "Install";

                        ProjectCostingSheetAddMaterialsInformation model = new ProjectCostingSheetAddMaterialsInformation(projectCostingSheetAddTDS);
                        model.Insert(0, materialId, unitOfMeasurement, quantity, costCad, totalCostCad, costUsd, totalCostUsd, false, companyId, process, description, startDate, endDate, function_, workFunctionConcat);

                        // Store tables
                        Session.Remove("materialsInformationDummy");
                        materialsInformation = (ProjectCostingSheetAddTDS.MaterialsInformationDataTable)model.Table;
                        Session["materialsInformation"] = materialsInformation;
                        Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

                        grdMaterials.DataBind();

                        StepMaterialInformationProcessGrid();
                    }
                    break;
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // STEP6 - MATERIAL INFORMATION - METHODS
        //
        private void StepMaterialInformationIn()
        {
            // Set instruction
            Label instruction = (Label)this.Master.FindControl("lblInstruction");
            instruction.Text = "Please verify Material information";

            // Load
            ProjectCostingSheetAddMaterialsInformation model = new ProjectCostingSheetAddMaterialsInformation(projectCostingSheetAddTDS);

            if (projectCostingSheetAddTDS.MaterialsInformation.Rows.Count <= 0)
            {
                ArrayList works = new ArrayList();
                if (cbxFullLengthLiningData.Checked) works.Add("Full Length Lining");
                if (cbxManholeRehabData.Checked) works.Add("Manhole Rehab");
                if (cbxPointRepairData.Checked) works.Add("Point Repairs");
                if (cbxJunctionLiningData.Checked) works.Add("Junction Lining");

                model.Load(works, int.Parse(hdfProjectId.Value), tkrdpFrom.SelectedDate.Value, tkrdpTo.SelectedDate.Value, int.Parse(hdfCompanyId.Value));
            }

            // Store tables
            Session.Remove("materialsInformationDummy");
            materialsInformation = (ProjectCostingSheetAddTDS.MaterialsInformationDataTable)model.Table;
            Session["materialsInformation"] = materialsInformation;
            Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

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

            if (projectGateway.GetCountryID(projectId) == 1) //Canada
            {
                // Materials grid
                grdMaterials.Columns[9].Visible = true;
                grdMaterials.Columns[10].Visible = true;
                grdMaterials.Columns[11].Visible = false;
                grdMaterials.Columns[12].Visible = false;

                lblMaterialsTotalCosts.Text = "Total Cost (CAD) : ";
                tbxMaterialsTotalCostsCAD.Visible = true;
                tbxMaterialsTotalCostsUSD.Visible = false;
            }
            else
            {
                // Materials grid
                grdMaterials.Columns[9].Visible = false;
                grdMaterials.Columns[10].Visible = false;
                grdMaterials.Columns[11].Visible = true;
                grdMaterials.Columns[12].Visible = true;

                lblMaterialsTotalCosts.Text = "Total Cost (USD) : ";
                tbxMaterialsTotalCostsCAD.Visible = false;
                tbxMaterialsTotalCostsUSD.Visible = true;
            }

            grdMaterials.DataBind();
            StepMaterialInformationProcessGrid();
        }
        private void UpdateDatabase()
        {
            DB.Open();
            DB.BeginTransaction();
            try
            {
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int loginId = Convert.ToInt32(Session["loginID"]);

                if (cbxEndConfirm.Checked)
                {
                    ProjectCostingSheetAddBasicInformation projectCostingSheetAddBasicInformation = new ProjectCostingSheetAddBasicInformation(projectCostingSheetAddTDS);
                    int costingSheetId = projectCostingSheetAddBasicInformation.Save(companyId);
                    hdfCostingSheetId.Value = costingSheetId.ToString();

                    // Save costs information
                    ProjectCostingSheetAddLabourHoursInformation projectCostingSheetAddLabourHoursInformation = new ProjectCostingSheetAddLabourHoursInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddLabourHoursInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddUnitsInformation projectCostingSheetAddUnitsInformation = new ProjectCostingSheetAddUnitsInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddUnitsInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddMaterialsInformation projectCostingSheetAddMaterialsInformation = new ProjectCostingSheetAddMaterialsInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddMaterialsInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddSubcontractorsInformation projectCostingSheetAddSubcontractorsInformation = new ProjectCostingSheetAddSubcontractorsInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddSubcontractorsInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddOtherCostsInformation projectCostingSheetAddOtherCostsInformation = new ProjectCostingSheetAddOtherCostsInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddOtherCostsInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddHotelsInformation projectCostingSheetAddHotelsInformation = new ProjectCostingSheetAddHotelsInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddHotelsInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddBondingsInformation projectCostingSheetAddBondingsInformation = new ProjectCostingSheetAddBondingsInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddBondingsInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddInsurancesInformation projectCostingSheetAddInsurancesInformation = new ProjectCostingSheetAddInsurancesInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddInsurancesInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddOtherCategoryInformation projectCostingSheetAddOtherCategoryInformation = new ProjectCostingSheetAddOtherCategoryInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddOtherCategoryInformation.Save(companyId, costingSheetId);

                    ProjectCostingSheetAddRevenueInformation projectCostingSheetAddRevenueInformation = new ProjectCostingSheetAddRevenueInformation(projectCostingSheetAddTDS);
                    projectCostingSheetAddRevenueInformation.Save(companyId, costingSheetId);

                    if (rbtnBeginTemplate.Checked)
                    {
                        ProjectCostingSheetTemplateInformation projectCostingSheetTemplateInformation = new ProjectCostingSheetTemplateInformation(projectCostingSheetAddTDS);
                        projectCostingSheetTemplateInformation.Save(companyId);
                    }
                }
                else
                {
                    if (cbxEndSave.Checked)
                    {
                        ProjectCostingSheetTemplateInformation projectCostingSheetTemplateInformation = new ProjectCostingSheetTemplateInformation(projectCostingSheetAddTDS);
                        projectCostingSheetTemplateInformation.Save(companyId);
                    }
                    else
                    {
                        if (rbtnBeginTemplate.Checked)
                        {
                            ProjectCostingSheetTemplateInformation projectCostingSheetTemplateInformation = new ProjectCostingSheetTemplateInformation(projectCostingSheetAddTDS);
                            projectCostingSheetTemplateInformation.Save(companyId);
                        }
                    }
                }

                DB.CommitTransaction();

                // Store datasets
                projectCostingSheetAddTDS.AcceptChanges();
                Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        protected void grdMaterials_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // Validate general data
            Page.Validate("materialsEdit");
            int projectId = Int32.Parse(hdfProjectId.Value);
            ProjectGateway projectGateway = new ProjectGateway();

            if (Page.IsValid)
            {
                // Validate costs
                projectGateway.LoadByProjectId(projectId);

                if (projectGateway.GetCountryID(projectId) == 1) //Canada
                {
                    Page.Validate("materialsCadEdit");
                }
                else
                {
                    Page.Validate("materialsUsdEdit");
                }
            }

            if (Page.IsValid)
            {
                int costingSheetId = (int)e.Keys["CostingSheetID"];
                int materialId = (int)e.Keys["MaterialID"];
                int refId = (int)e.Keys["RefID"];
                int companyId = Int32.Parse(hdfCompanyId.Value);

                string unitOfMeasurement = ((DropDownList)grdMaterials.Rows[e.RowIndex].Cells[0].FindControl("ddlUnitOfMeasurementMaterialsEdit")).SelectedValue;
                double quantity = double.Parse(((TextBox)grdMaterials.Rows[e.RowIndex].Cells[0].FindControl("tbxQuantityEdit")).Text.Trim());

                decimal costCad = 0.0M;
                decimal totalCostCad = 0.0M;
                decimal costUsd = 0.0M;
                decimal totalCostUsd = 0.0M;

                if (projectGateway.GetCountryID(projectId) == 1) //Canada
                {
                    costCad = Decimal.Parse(((TextBox)grdMaterials.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)grdMaterials.Rows[e.RowIndex].Cells[0].FindControl("tbxCostUSDEdit")).Text.Trim());
                    totalCostUsd = (costUsd * decimal.Parse(quantity.ToString()));
                    totalCostUsd = Decimal.Round(totalCostUsd, 2);
                }

                LFSLive.DA.Resources.Materials.MaterialsGateway m = new LiquiForce.LFSLive.DA.Resources.Materials.MaterialsGateway();
                m.LoadByMaterialId(materialId, companyId);
                string process = m.GetType(materialId);
                string description = m.GetDescription(materialId);

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

                string workFunctionConcat = "Full Length . Install";
                string function_ = "Install";

                // Update data
                ProjectCostingSheetAddMaterialsInformation model = new ProjectCostingSheetAddMaterialsInformation(projectCostingSheetAddTDS);
                model.Update(costingSheetId, materialId, refId, unitOfMeasurement, quantity, costCad, totalCostCad, costUsd, totalCostUsd, false, companyId, process, description, startDate, endDate, function_, workFunctionConcat);

                // Store tables
                Session.Remove("materialsInformationDummy");
                materialsInformation = (ProjectCostingSheetAddTDS.MaterialsInformationDataTable)model.Table;
                Session["materialsInformation"] = materialsInformation;
                Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

                StepMaterialInformationProcessGrid();
            }
            else
            {
                e.Cancel = true;
            }
        }