private bool StepSelectSectionsNext()
        {
            StepSelectSectionsProcessGrid();

            // create dataset
            ProjectAddSectionsTDS dataSet = new ProjectAddSectionsTDS();
            dataSet.ProjectAddSectionsSearch.Merge(projectAddSectionsSearch, true);
            dataSet.ProjectAddSectionsTemp.Merge(projectAddSectionsTemp, true);

            // process new sections
            ProjectAddSectionsTemp model = new ProjectAddSectionsTemp(dataSet);
            model.ProcessSearch();

            // get changes
            projectAddSectionsSearch.Rows.Clear();
            projectAddSectionsTemp.Rows.Clear();
            projectAddSectionsTemp.Merge(model.Table);

            // store tables
            Session.Remove("projectAddSectionsSearchDummy");
            Session["projectAddSectionsSearch"] = projectAddSectionsSearch;
            Session["projectAddSectionsTemp"] = projectAddSectionsTemp;

            return true;
        }
        // ////////////////////////////////////////////////////////////////////////
        // STEP9 - SUMMARY - METHODS
        //
        private void StepSummaryIn()
        {
            // Set instruction
            Label instruction = (Label)this.Master.FindControl("lblInstruction");
            instruction.Text = "Summary";

            // Initialize summary
            ProjectAddSectionsTDS dataSet = new ProjectAddSectionsTDS();
            dataSet.ProjectAddSectionsTemp.Merge(projectAddSectionsTemp, true);
            ProjectAddSectionsTemp model = new ProjectAddSectionsTemp(dataSet);

            tbxSummary.Text = model.GetSectionsSummary(hdfWorkType.Value);
        }
        // ////////////////////////////////////////////////////////////////////////
        // STEP5 - NEW INTERMEDIATE SECTIONS - AUXILIARY EVENTS
        //
        protected void cvSectionId_ServerValidate(object source, ServerValidateEventArgs args)
        {
            ProjectAddSectionsTDS dataSet = new ProjectAddSectionsTDS();
            dataSet.ProjectAddSectionsNew.Merge(projectAddSectionsNew, true);
            dataSet.ProjectAddSectionsTemp.Merge(projectAddSectionsTemp, true);

            ProjectAddSectionsNew projectAddSectionsNewModel = new ProjectAddSectionsNew(dataSet);
            ProjectAddSectionsTemp projectAddSectionsTempModel = new ProjectAddSectionsTemp(dataSet);

            // Initialize
            CustomValidator cvSectionId = (CustomValidator)source;
            args.IsValid = true;

            // verify in this step
            if (projectAddSectionsNewModel.ExistsBySectionId(args.Value))
            {
                cvSectionId.Text = "Duplicated section. (you already have this section here)";
                args.IsValid = false;
            }

            // verify before added or selected
            if (args.IsValid)
            {
                if (projectAddSectionsTempModel.ExistsBySectionId(args.Value))
                {
                    cvSectionId.Text = "Duplicated section. (this section was already created or added previously)";
                    args.IsValid = false;
                }
            }

            // verify in project with specific work
            if (args.IsValid)
            {
                string workType;
                switch (hdfWorkType.Value)
                {
                    case "All":
                        workType = "%";
                        break;

                    case "Rehab Assessment":
                        workType = "Rehab Assessment";
                        break;

                    case "Full Length Lining":
                        workType = "Full Length Lining";
                        break;

                    case "Point Repairs":
                        workType = "Point Repairs";
                        break;

                    case "Junction Lining":
                        workType = "Junction Lining Section";
                        break;

                    case "Manhole Rehabilitation":
                        workType = "%";
                        break;

                    default:
                        throw new Exception("Erroneus work type in Add Section query string.");
                }

                WorkGateway workGateway = new WorkGateway(null);
                if (workGateway.ExistsSectionByProjectIdSectionIdWorkType(int.Parse(hdfProjectId.Value), args.Value, workType, int.Parse(hdfCompanyId.Value)))
                {
                    cvSectionId.Text = "This section is already associated with this project. (if you would like to see it go to the Sections data of this project)";
                    args.IsValid = false;
                }
            }

            // verify in project general
            if (args.IsValid)
            {
                Int64? countryId = int.Parse(hdfCountryId.Value);
                Int64? provinceId = null; if (hdfProvinceId.Value != "") provinceId = Int64.Parse(hdfProvinceId.Value);
                Int64? countyId = null; if (hdfCountyId.Value != "") countyId = Int64.Parse(hdfCountyId.Value);
                Int64? cityId = null; if (hdfCityId.Value != "") cityId = Int64.Parse(hdfCityId.Value);
                int companyId = int.Parse(hdfCompanyId.Value);

                AssetSewerSectionGateway assetSewerSectionGateway = new AssetSewerSectionGateway(null);
                if (assetSewerSectionGateway.ExistsByCountryIdProvinceIdCountyIdCityIdSectionId(countryId, provinceId, countyId, cityId, args.Value, companyId))
                {
                    cvSectionId.Text = "This section is already in the database. (you can try adding it using the Add Existing Sections option)";
                    args.IsValid = false;
                }
            }
        }
        private bool StepNewSectionsNext()
        {
            // Update dataset
            ProjectAddSectionsTDS dataSet = new ProjectAddSectionsTDS();
            dataSet.ProjectAddSectionsNew.Merge(projectAddSectionsNew, true);
            ProjectAddSectionsNew model = new ProjectAddSectionsNew(dataSet);

            // Conditions Gridview, if the gridview is edition mode
            if (grdProjectAddSectionsNew.EditIndex >= 0)
            {
                grdProjectAddSectionsNew.UpdateRow(grdProjectAddSectionsNew.EditIndex, false);
            }

            if (ValidateProjectAddSectionsFooter())
            {
                Page.Validate("AddSectionsAdd");
                if (!Page.IsValid)
                {
                    return false;
                }
                else
                {
                    bool cbxRA = ((CheckBox)grdProjectAddSectionsNew.FooterRow.FindControl("cbxRehabAssessmentWorkAdd")).Checked;
                    bool cbxFLL = ((CheckBox)grdProjectAddSectionsNew.FooterRow.FindControl("cbxFullLengthLiningWorkAdd")).Checked;
                    bool cbxPR = ((CheckBox)grdProjectAddSectionsNew.FooterRow.FindControl("cbxPointRepairsWorkAdd")).Checked;
                    bool cbxJL = ((CheckBox)grdProjectAddSectionsNew.FooterRow.FindControl("cbxJunctionLiningWorkAdd")).Checked;

                    bool cbxMHRehabUsmh = ((CheckBox)grdProjectAddSectionsNew.FooterRow.FindControl("cbxMHRehabUsmhAdd")).Checked;
                    bool cbxMHRehabDsmh = ((CheckBox)grdProjectAddSectionsNew.FooterRow.FindControl("cbxMHRehabDsmhAdd")).Checked;

                    if (cbxRA || cbxFLL || cbxJL || cbxPR)
                    {
                        // Get values
                        int companyId = Int32.Parse(hdfCompanyId.Value);
                        string street = ((TextBox)grdProjectAddSectionsNew.FooterRow.FindControl("tbxStreetAdd")).Text;
                        string usmh = ((TextBox)grdProjectAddSectionsNew.FooterRow.FindControl("tbxUsmhAdd")).Text;
                        string dsmh = ((TextBox)grdProjectAddSectionsNew.FooterRow.FindControl("tbxDsmhAdd")).Text;
                        string mapSize = ((TextBox)grdProjectAddSectionsNew.FooterRow.FindControl("tbxMapSizeAdd")).Text;
                        string mapLength = ((TextBox)grdProjectAddSectionsNew.FooterRow.FindControl("tbxMapLengthAdd")).Text;

                        string clientId = "";
                        if (((TextBox)grdProjectAddSectionsNew.FooterRow.FindControl("tbxClientIdAdd")).Text.Trim() != "")
                        {
                            clientId = ((TextBox)grdProjectAddSectionsNew.FooterRow.FindControl("tbxClientIdAdd")).Text;
                        }

                        string sectionMaterial = "";
                        if (((DropDownList)grdProjectAddSectionsNew.FooterRow.FindControl("ddlSectionMaterialAdd")).SelectedValue != "-1")
                        {
                            sectionMaterial = ((DropDownList)grdProjectAddSectionsNew.FooterRow.FindControl("ddlSectionMaterialAdd")).SelectedValue;
                        }

                        string usmhStreet = ((TextBox)grdProjectAddSectionsNew.FooterRow.FindControl("tbxUsmhStreetAdd")).Text.Trim();
                        string usmhLatitude = ((TextBox)grdProjectAddSectionsNew.FooterRow.FindControl("tbxUsmhLatitudeAdd")).Text.Trim();
                        string usmhLongitude = ((TextBox)grdProjectAddSectionsNew.FooterRow.FindControl("tbxUsmhLongitudeAdd")).Text.Trim();
                        string usmhShape = ((DropDownList)grdProjectAddSectionsNew.FooterRow.FindControl("ddlUsmhShapeAdd")).SelectedValue;
                        string usmhLocation = ((DropDownList)grdProjectAddSectionsNew.FooterRow.FindControl("ddlUsmhLocationAdd")).SelectedValue;
                        int? usmhConditionRating = null;
                        if (((DropDownList)grdProjectAddSectionsNew.FooterRow.FindControl("ddlUsmhConditionRatingAdd")).SelectedValue != "-1")
                        {
                            usmhConditionRating = Int32.Parse(((DropDownList)grdProjectAddSectionsNew.FooterRow.FindControl("ddlUsmhConditionRatingAdd")).SelectedValue);
                        }
                        int? usmhMaterialId = null;
                        string usmhMaterial = "";
                        if (((DropDownList)grdProjectAddSectionsNew.FooterRow.FindControl("ddlUsmhMaterialAdd")).SelectedValue != "-1")
                        {
                            usmhMaterialId = Int32.Parse(((DropDownList)grdProjectAddSectionsNew.FooterRow.FindControl("ddlUsmhMaterialAdd")).SelectedValue);
                            AssetSewerMHMaterialTypeGateway assetSewerMHMaterialTypeGatewayForUsmh = new AssetSewerMHMaterialTypeGateway();
                            assetSewerMHMaterialTypeGatewayForUsmh.LoadByMaterialId((int)usmhMaterialId, companyId);
                            usmhMaterial = assetSewerMHMaterialTypeGatewayForUsmh.GetMaterialType((int)usmhMaterialId);
                        }

                        string dsmhStreet = ((TextBox)grdProjectAddSectionsNew.FooterRow.FindControl("tbxDsmhStreetAdd")).Text.Trim();
                        string dsmhLatitude = ((TextBox)grdProjectAddSectionsNew.FooterRow.FindControl("tbxDsmhLatitudeAdd")).Text.Trim();
                        string dsmhLongitude = ((TextBox)grdProjectAddSectionsNew.FooterRow.FindControl("tbxDsmhLongitudeAdd")).Text.Trim();
                        string dsmhShape = ((DropDownList)grdProjectAddSectionsNew.FooterRow.FindControl("ddlDsmhShapeAdd")).SelectedValue;
                        string dsmhLocation = ((DropDownList)grdProjectAddSectionsNew.FooterRow.FindControl("ddlDsmhLocationAdd")).SelectedValue;
                        int? dsmhConditionRating = null;
                        if (((DropDownList)grdProjectAddSectionsNew.FooterRow.FindControl("ddlDsmhConditionRatingAdd")).SelectedValue != "-1")
                        {
                            dsmhConditionRating = Int32.Parse(((DropDownList)grdProjectAddSectionsNew.FooterRow.FindControl("ddlDsmhConditionRatingAdd")).SelectedValue);
                        }
                        int? dsmhMaterialId = null;
                        string dsmhMaterial = "";
                        if (((DropDownList)grdProjectAddSectionsNew.FooterRow.FindControl("ddlDsmhMaterialAdd")).SelectedValue != "-1")
                        {
                            dsmhMaterialId = Int32.Parse(((DropDownList)grdProjectAddSectionsNew.FooterRow.FindControl("ddlDsmhMaterialAdd")).SelectedValue);
                            AssetSewerMHMaterialTypeGateway assetSewerMHMaterialTypeGatewayForDsmh = new AssetSewerMHMaterialTypeGateway();
                            assetSewerMHMaterialTypeGatewayForDsmh.LoadByMaterialId((int)dsmhMaterialId, companyId);
                            dsmhMaterial = assetSewerMHMaterialTypeGatewayForDsmh.GetMaterialType((int)dsmhMaterialId);
                        }

                        // ... get structure data
                        string usmhTopDiameter = "";
                        string usmhTopDepth = "";
                        string usmhDownDiameter = "";
                        string usmhDownDepth = "";
                        int? usmhManholeRugs = null;
                        string usmhRectangle1LongSide = "";
                        string usmhRectangle1ShortSide = "";
                        string usmhRectangle2LongSide = "";
                        string usmhRectangle2ShortSide = "";
                        string usmhTotalSurfaceArea = "";
                        string dsmhTopDiameter = "";
                        string dsmhTopDepth = "";
                        string dsmhDownDiameter = "";
                        string dsmhDownDepth = "";
                        int? dsmhManholeRugs = null;
                        string dsmhRectangle1LongSide = "";
                        string dsmhRectangle1ShortSide = "";
                        string dsmhRectangle2LongSide = "";
                        string dsmhRectangle2ShortSide = "";
                        string dsmhTotalSurfaceArea = "";

                        int sectionId = model.Table.Rows.Count + 1;

                        // Insert section
                        if ((street != "") || (usmh != "") || (dsmh != "") || (mapSize != "") || (mapLength != ""))
                        {
                            model.Insert(sectionId.ToString(), street, usmh, dsmh, cbxRA, cbxFLL, cbxPR, cbxJL, false, sectionId.ToString(), mapSize, mapLength, usmhStreet, usmhLatitude, usmhLongitude, usmhShape, usmhLocation, usmhConditionRating, usmhMaterialId, usmhMaterial, usmhTopDiameter, usmhTopDepth, usmhDownDiameter, usmhDownDepth, usmhManholeRugs, usmhRectangle1LongSide, usmhRectangle1ShortSide, usmhRectangle2LongSide, usmhRectangle2ShortSide, usmhTotalSurfaceArea, dsmhStreet, dsmhLatitude, dsmhLongitude, dsmhShape, dsmhLocation, dsmhConditionRating, dsmhMaterialId, dsmhMaterial, dsmhTopDiameter, dsmhTopDepth, dsmhDownDiameter, dsmhDownDepth, dsmhManholeRugs, dsmhRectangle1LongSide, dsmhRectangle1ShortSide, dsmhRectangle2LongSide, dsmhRectangle2ShortSide, dsmhTotalSurfaceArea, sectionMaterial, clientId, cbxMHRehabUsmh, cbxMHRehabDsmh);
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }

            // create dataset for temp
            dataSet.ProjectAddSectionsTemp.Merge(projectAddSectionsTemp, true);

            // process new sections
            ProjectAddSectionsTemp modelTemp = new ProjectAddSectionsTemp(dataSet);
            modelTemp.ProcessNew();

            // get changes
            projectAddSectionsNew.Rows.Clear();
            projectAddSectionsTemp.Rows.Clear();
            projectAddSectionsTemp.Merge(modelTemp.Table);

            // store tables
            Session.Remove("projectAddSectionsNewDummy");
            Session["projectAddSectionsNew"] = projectAddSectionsNew;
            Session["projectAddSectionsTemp"] = projectAddSectionsTemp;

            return true;
        }
        private bool StepNewIntermediateSectionsNext()
        {
            Page.Validate("intermediateData");
            if (Page.IsValid)
            {
                // Saving new data
                string secuentialPart = ddlNewIntermediateSectionId.SelectedValue.ToString();
                string street = tbxNewIntermediateStreet.Text.Trim();
                string usmh = tbxNewIntermediateUsmh.Text.Trim();
                string dsmh = tbxNewIntermediateDsmh.Text.Trim();
                string mapSize = tbxNewIntermediateMapSize.Text.Trim();
                string mapLength = tbxNewIntermediateMapLength.Text.Trim();

                ProjectAddSectionsTDS dataSet = new ProjectAddSectionsTDS();
                dataSet.ProjectAddSectionsNew.Merge(projectAddSectionsNew, true);
                ProjectAddSectionsNew intermediateData = new ProjectAddSectionsNew(dataSet);

                // ... Insert section
                string countryId = "0"; if (hdfCountryId.Value != "") countryId = hdfCountryId.Value;
                string provinceId = "0"; if (hdfProvinceId.Value != "") provinceId = hdfProvinceId.Value;
                string countyId = "0"; if (hdfCountyId.Value != "") countyId = hdfCountyId.Value;
                string cityId = "0"; if (hdfCityId.Value != "") cityId = hdfCityId.Value;

                string sectionId = countryId + "." + provinceId + "." + countyId + "." + cityId;
                sectionId = sectionId + "-" + secuentialPart;

                string flowOrderId = sectionId.Substring(sectionId.Length - 6, 6);

                bool rehabAssesswork = false; if (hdfWorkType.Value == "Rehab Assessment") rehabAssesswork = true;
                bool fullLengthLiningWork = false; if (hdfWorkType.Value == "Full Length Lining") fullLengthLiningWork = true;
                bool pointRepairsWork = false; if (hdfWorkType.Value == "Point Repairs") pointRepairsWork = true;
                bool junctionLiningWork = false; if (hdfWorkType.Value == "Junction Lining") junctionLiningWork = true;

                intermediateData.Insert(sectionId, street, usmh, dsmh, rehabAssesswork, fullLengthLiningWork, pointRepairsWork, junctionLiningWork, false, flowOrderId, mapSize, mapLength, "", "", "", "", "", null, null, "", "", "", "", "", null, "", "", "", "", "", "", "", "", "", "", null, null, "", "", "", "", "", null, "", "", "", "", "", "", "", false, false);

                // create temp dataset
                dataSet.ProjectAddSectionsTemp.Merge(projectAddSectionsTemp, true);

                // process new sections
                ProjectAddSectionsTemp model = new ProjectAddSectionsTemp(dataSet);
                model.ProcessNew();

                // get changes
                projectAddSectionsNew.Rows.Clear();
                projectAddSectionsTemp.Rows.Clear();
                projectAddSectionsTemp.Merge(model.Table);

                // store tables
                Session.Remove("projectAddSectionsNewDummy");
                Session["projectAddSectionsNew"] = projectAddSectionsNew;
                Session["projectAddSectionsTemp"] = projectAddSectionsTemp;
                return true;

            }
            return false;
        }
        private void Save()
        {
            // process works
            ProjectAddSectionsTDS dataSet = new ProjectAddSectionsTDS();
            dataSet.ProjectAddSectionsTemp.Merge(projectAddSectionsTemp, true);
            ProjectAddSectionsTemp model = new ProjectAddSectionsTemp(dataSet);

            // get parameters
            Int64 countryId = int.Parse(hdfCountryId.Value);
            Int64? provinceId = null; if (hdfProvinceId.Value != "") provinceId = Int64.Parse(hdfProvinceId.Value);
            Int64? countyId = null; if (hdfCountyId.Value != "") countyId = Int64.Parse(hdfCountyId.Value);
            Int64? cityId = null; if (hdfCityId.Value != "") cityId = Int64.Parse(hdfCityId.Value);

            // save to database
            DB.Open();
            DB.BeginTransaction();
            try
            {
                model.Save(int.Parse(hdfProjectId.Value), countryId, provinceId, countyId, cityId, int.Parse(hdfCompanyId.Value));

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

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        protected void grdRemove_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = (int)e.Keys["ID"];

            ProjectAddSectionsTDS dataSet = new ProjectAddSectionsTDS();
            dataSet.ProjectAddSectionsTemp.Merge(projectAddSectionsTemp, true);
            ProjectAddSectionsTemp model = new ProjectAddSectionsTemp(dataSet);

            model.Delete(id);
            Session["projectAddSectionsTemp"] = dataSet.ProjectAddSectionsTemp;
        }