protected void grdProjectAddSectionsNew_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // Edit controls
            if ((e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState == DataControlRowState.Edit) || (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate))))
            {
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int id = Int32.Parse(((Label)e.Row.FindControl("lblID")).Text);

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

                ProjectAddSectionsNewGateway projectAddSectionsNewGatewayForGrid = new ProjectAddSectionsNewGateway(dataSet);

                if (projectAddSectionsNewGatewayForGrid.Table.Rows.Count > 0)
                {
                    ((DropDownList)e.Row.FindControl("ddlSectionMaterialEdit")).SelectedValue = projectAddSectionsNewGatewayForGrid.GetSectionMaterial(id).ToString();

                    ((DropDownList)e.Row.FindControl("ddlUsmhShapeEdit")).SelectedValue = projectAddSectionsNewGatewayForGrid.GetUsmhShape(id);
                    ((DropDownList)e.Row.FindControl("ddlUsmhLocationEdit")).SelectedValue = projectAddSectionsNewGatewayForGrid.GetUsmhLocation(id);
                    ((DropDownList)e.Row.FindControl("ddlUsmhConditionRatingEdit")).SelectedValue = projectAddSectionsNewGatewayForGrid.GetUsmhConditionRating(id).ToString();
                    ((DropDownList)e.Row.FindControl("ddlUsmhMaterialEdit")).SelectedValue = projectAddSectionsNewGatewayForGrid.GetUsmhMaterialId(id).ToString();

                    ((DropDownList)e.Row.FindControl("ddlDsmhShapeEdit")).SelectedValue = projectAddSectionsNewGatewayForGrid.GetDsmhShape(id);
                    ((DropDownList)e.Row.FindControl("ddlDsmhLocationEdit")).SelectedValue = projectAddSectionsNewGatewayForGrid.GetDsmhLocation(id);
                    ((DropDownList)e.Row.FindControl("ddlDsmhConditionRatingEdit")).SelectedValue = projectAddSectionsNewGatewayForGrid.GetDsmhConditionRating(id).ToString();
                    ((DropDownList)e.Row.FindControl("ddlDsmhMaterialEdit")).SelectedValue = projectAddSectionsNewGatewayForGrid.GetDsmhMaterialId(id).ToString();
                }
            }

            // Footer Item
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                switch (hdfWorkType.Value)
                {
                    case "All":
                        ((CheckBox)e.Row.FindControl("cbxRehabAssessmentWorkAdd")).Checked = true;
                        ((CheckBox)e.Row.FindControl("cbxFullLengthLiningWorkAdd")).Checked = true;
                        ((CheckBox)e.Row.FindControl("cbxPointRepairsWorkAdd")).Checked = true;
                        ((CheckBox)e.Row.FindControl("cbxJunctionLiningWorkAdd")).Checked = true;
                        ((CheckBox)e.Row.FindControl("cbxMHRehabUsmhAdd")).Checked = true;
                        ((CheckBox)e.Row.FindControl("cbxMHRehabDsmhAdd")).Checked = true;
                        break;

                    case "Rehab Assessment":
                        ((CheckBox)e.Row.FindControl("cbxRehabAssessmentWorkAdd")).Checked = true;
                        ((CheckBox)e.Row.FindControl("cbxFullLengthLiningWorkAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxPointRepairsWorkAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxJunctionLiningWorkAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxMHRehabUsmhAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxMHRehabDsmhAdd")).Checked = false;
                        break;

                    case "Full Length Lining":
                        ((CheckBox)e.Row.FindControl("cbxRehabAssessmentWorkAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxFullLengthLiningWorkAdd")).Checked = true;
                        ((CheckBox)e.Row.FindControl("cbxPointRepairsWorkAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxJunctionLiningWorkAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxMHRehabUsmhAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxMHRehabDsmhAdd")).Checked = false;
                        break;

                    case "Point Repairs":
                        ((CheckBox)e.Row.FindControl("cbxRehabAssessmentWorkAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxFullLengthLiningWorkAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxPointRepairsWorkAdd")).Checked = true;
                        ((CheckBox)e.Row.FindControl("cbxJunctionLiningWorkAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxMHRehabUsmhAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxMHRehabDsmhAdd")).Checked = false;
                        break;

                    case "Junction Lining":
                        ((CheckBox)e.Row.FindControl("cbxRehabAssessmentWorkAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxFullLengthLiningWorkAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxPointRepairsWorkAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxJunctionLiningWorkAdd")).Checked = true;
                        ((CheckBox)e.Row.FindControl("cbxMHRehabUsmhAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxMHRehabDsmhAdd")).Checked = false;
                        break;

                    case "Manhole Rehabilitation":
                        ((CheckBox)e.Row.FindControl("cbxRehabAssessmentWorkAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxFullLengthLiningWorkAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxPointRepairsWorkAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxJunctionLiningWorkAdd")).Checked = false;
                        ((CheckBox)e.Row.FindControl("cbxMHRehabUsmhAdd")).Checked = true;
                        ((CheckBox)e.Row.FindControl("cbxMHRehabDsmhAdd")).Checked = true;
                        break;
                }
            }
        }
        private void StepSelectSectionsProcessGrid()
        {
            ProjectAddSectionsTDS dataSet = new ProjectAddSectionsTDS();
            dataSet.ProjectAddSectionsSearch.Merge(projectAddSectionsSearch, true);
            ProjectAddSectionsSearch model = new ProjectAddSectionsSearch(dataSet);

            // update rows
            if (Session["projectAddSectionsSearchDummy"] == null)
            {
                foreach (GridViewRow row in grdProjectAddSectionsSearch.Rows)
                {
                    string sectionId = ((Label)row.FindControl("lblSectionID")).Text.Trim();
                    bool selected = ((CheckBox)row.FindControl("cbxSelected")).Checked;

                    bool rehabAssessment = ((CheckBox)row.FindControl("cbxRehabAssessmentPrevWork")).Checked;
                    bool fullLengthLining = ((CheckBox)row.FindControl("cbxFullLengthLiningPrevWork")).Checked;
                    bool pointRepairs = ((CheckBox)row.FindControl("cbxPointRepairsPrevWork")).Checked;
                    bool junctionLining = ((CheckBox)row.FindControl("cbxJunctionLiningPrevWork")).Checked;

                    bool rehabAssessmentPrevWork = ((CheckBox)row.FindControl("cbxRehabAssessmentPrevWorkReadOnly")).Checked;
                    bool fullLengthLiningPrevWork = ((CheckBox)row.FindControl("cbxFullLengthLiningPrevWorkReadOnly")).Checked;
                    bool pointRepairsPrevWork = ((CheckBox)row.FindControl("cbxPointRepairsPrevWorkReadOnly")).Checked;
                    bool junctionLiningPrevWork = ((CheckBox)row.FindControl("cbxJunctionLiningPrevWorkReadOnly")).Checked;

                    model.UpdateBySectionId(sectionId, rehabAssessment, fullLengthLining, pointRepairs, junctionLining, selected, rehabAssessmentPrevWork, fullLengthLiningPrevWork, pointRepairsPrevWork, junctionLiningPrevWork);
                }

                model.Table.AcceptChanges();

                projectAddSectionsSearch = (ProjectAddSectionsTDS.ProjectAddSectionsSearchDataTable)model.Table;
                Session["projectAddSectionsSearch"] = projectAddSectionsSearch;
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // 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);
        }
        private void StepSelectForIntermediateSectionsProcessGrid()
        {
            ProjectAddSectionsTDS dataSet = new ProjectAddSectionsTDS();
            dataSet.ProjectAddSectionsSearch.Merge(projectAddSectionsSearch, true);
            ProjectAddSectionsSearch model = new ProjectAddSectionsSearch(dataSet);

            // update rows
            if (Session["projectAddSectionsSearchDummy"] == null)
            {
                foreach (GridViewRow row in grdSelectForIntermediateSectionsSearch.Rows)
                {
                    string sectionId = ((Label)row.FindControl("lblSectionID_")).Text.Trim();
                    bool selected = ((CheckBox)row.FindControl("cbxSelected_")).Checked;

                    model.UpdateBySectionId(sectionId, false, false, false, false, selected, false, false, false, false);
                }

                model.Table.AcceptChanges();

                projectAddSectionsSearch = (ProjectAddSectionsTDS.ProjectAddSectionsSearchDataTable)model.Table;
                Session["projectAddSectionsSearch"] = projectAddSectionsSearch;
            }
        }
        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;
        }
 /// <summary>
 /// InitData
 /// </summary>
 protected override void InitData()
 {
     _data = new ProjectAddSectionsTDS();
 }
        private bool StepSelectForIntermediateSectionsNext()
        {
            ProjectAddSectionsTDS dataSet = new ProjectAddSectionsTDS();
            dataSet.ProjectAddSectionsSearch.Merge(projectAddSectionsSearch, true);
            ProjectAddSectionsSearch model = new ProjectAddSectionsSearch(dataSet);

            if (model.Table.Rows.Count > 0)
            {
                Page.Validate("selectForIntermediateSection");
                if (Page.IsValid)
                {
                    StepSelectForIntermediateSectionsProcessGrid();

                    if (Session["projectAddSectionsSearchDummy"] == null)
                    {
                        foreach (GridViewRow row in grdSelectForIntermediateSectionsSearch.Rows)
                        {
                            string sectionId = ((Label)row.FindControl("lblSectionID_")).Text.Trim();
                            bool selected = ((CheckBox)row.FindControl("cbxSelected_")).Checked;
                            if (selected)
                            {
                                hdfPrimarySectionId.Value = sectionId;
                            }
                        }
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }

            return false;
        }
        // ////////////////////////////////////////////////////////////////////////
        // 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 StepSearchIntermediateSectionsNext()
        {
            // Get where clause and order by clause
            string whereClause = GetIntermediateWhereClause();

            string orderByClause = GetIntermediateOderByClause();

            // Search
            ProjectAddSectionsTDS dataSet = new ProjectAddSectionsTDS();
            dataSet.ProjectAddSectionsSearch.Merge(projectAddSectionsSearch, true);
            ProjectAddSectionsSearch model = new ProjectAddSectionsSearch(dataSet);

            model.Load(whereClause, orderByClause);

            // Store tables
            projectAddSectionsSearch = (ProjectAddSectionsTDS.ProjectAddSectionsSearchDataTable)model.Table;
            Session["projectAddSectionsSearch"] = projectAddSectionsSearch;

            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 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 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);
            }
        }
        private void GrdProjectAddSectionsNewAdd()
        {
            if (ValidateProjectAddSectionsFooter())
            {
                Page.Validate("AddSectionsAdd");
                if (Page.IsValid)
                {
                    // 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;
                    }

                    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;

                    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 = "";

                    // Update datasets
                    ProjectAddSectionsTDS dataSet = new ProjectAddSectionsTDS();
                    dataSet.ProjectAddSectionsNew.Merge(projectAddSectionsNew, true);
                    ProjectAddSectionsNew model = new ProjectAddSectionsNew(dataSet);
                    int sectionId = model.Table.Rows.Count + 1;

                    // Insert section
                    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);

                    // Store data
                    Session.Remove("projectAddSectionsNewDummy");
                    Session["projectAddSectionsNew"] = dataSet.ProjectAddSectionsNew;

                    grdProjectAddSectionsNew.DataBind();

                    grdProjectAddSectionsNew.PageIndex = grdProjectAddSectionsNew.PageCount - 1;
                }
            }
        }
        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;
        }
        protected void grdProjectAddSectionsNew_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // ProjectAddSectionsNew Gridview, if the gridview is edition mode
            if (grdProjectAddSectionsNew.EditIndex >= 0)
            {
                grdProjectAddSectionsNew.UpdateRow(grdProjectAddSectionsNew.EditIndex, true);
            }

            // Delete section
            int id = (int)e.Keys["ID"];

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

            model.Delete(id);
            Session["projectAddSectionsNew"] = dataSet.ProjectAddSectionsNew;
        }
        private bool StepSearchSectionsNext()
        {
            // Get where clause and order by clasue
            string whereClause = GetWhereClause();
            string orderByClause = GetOderByClause();

            // Search
            ProjectAddSectionsTDS dataSet = new ProjectAddSectionsTDS();
            dataSet.ProjectAddSectionsSearch.Merge(projectAddSectionsSearch, true);
            ProjectAddSectionsSearch model = new ProjectAddSectionsSearch(dataSet);

            model.Load(whereClause, orderByClause);

            // Review Previous Work
            int projectId = Int32.Parse(hdfProjectId.Value);
            int companyId = Int32.Parse(hdfCompanyId.Value);

            model.UpdatePreviousWorks(projectId, companyId);

            // Store tables
            projectAddSectionsSearch = (ProjectAddSectionsTDS.ProjectAddSectionsSearchDataTable) model.Table;
            Session["projectAddSectionsSearch"] = projectAddSectionsSearch;

            return true;
        }
        protected void grdProjectAddSectionsNew_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // Update section
            Page.Validate("AddSectionsUpdate");
            if (Page.IsValid)
            {
                int id = (int)e.Keys["ID"];
                int companyId = Int32.Parse(hdfCompanyId.Value);
                string sectionId = ((Label)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[0].FindControl("lblSectionIdEdit")).Text;
                string street = ((TextBox)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("tbxStreetEdit")).Text;
                string usmh = ((TextBox)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("tbxUsmhEdit")).Text;
                string dsmh = ((TextBox)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("tbxDsmhEdit")).Text;
                string mapSize = ((TextBox)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("tbxMapSizeEdit")).Text;
                string mapLength = ((TextBox)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("tbxMapLengthEdit")).Text;

                string clientId = "";
                if (((TextBox)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("tbxClientIdEdit")).Text.Trim() != "")
                {
                    clientId = ((TextBox)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("tbxClientIdEdit")).Text;
                }

                string sectionMaterial = "";
                if (((DropDownList)grdProjectAddSectionsNew.Rows[e.RowIndex].FindControl("ddlSectionMaterialEdit")).SelectedValue != "-1")
                {
                    sectionMaterial = ((DropDownList)grdProjectAddSectionsNew.Rows[e.RowIndex].FindControl("ddlSectionMaterialEdit")).SelectedValue;
                }

                bool cbxRA = ((CheckBox)grdProjectAddSectionsNew.Rows[e.RowIndex].FindControl("cbxRehabAssessmentWorkEdit")).Checked;
                bool cbxFLL = ((CheckBox)grdProjectAddSectionsNew.Rows[e.RowIndex].FindControl("cbxFullLengthLiningWorkEdit")).Checked;
                bool cbxPR = ((CheckBox)grdProjectAddSectionsNew.Rows[e.RowIndex].FindControl("cbxPointRepairsWorkEdit")).Checked;
                bool cbxJL = ((CheckBox)grdProjectAddSectionsNew.Rows[e.RowIndex].FindControl("cbxJunctionLiningWorkEdit")).Checked;

                bool cbxMHRehabUsmh = ((CheckBox)grdProjectAddSectionsNew.Rows[e.RowIndex].FindControl("cbxMHRehabUsmhEdit")).Checked;
                bool cbxMHRehabDsmh = ((CheckBox)grdProjectAddSectionsNew.Rows[e.RowIndex].FindControl("cbxMHRehabDsmhEdit")).Checked;

                string usmhStreet = ((TextBox)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("tbxUsmhStreetEdit")).Text.Trim();
                string usmhLatitude = ((TextBox)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("tbxUsmhLatitudeEdit")).Text.Trim();
                string usmhLongitude = ((TextBox)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("tbxUsmhLongitudeEdit")).Text.Trim();
                string usmhShape = ((DropDownList)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("ddlUsmhShapeEdit")).SelectedValue;
                string usmhLocation = ((DropDownList)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("ddlUsmhLocationEdit")).SelectedValue;
                int? usmhConditionRating = null;
                if (((DropDownList)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("ddlUsmhConditionRatingEdit")).SelectedValue != "-1")
                {
                    usmhConditionRating = Int32.Parse(((DropDownList)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("ddlUsmhConditionRatingEdit")).SelectedValue);
                }
                int? usmhMaterialId = null;
                string usmhMaterial = "";
                if (((DropDownList)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("ddlUsmhMaterialEdit")).SelectedValue != "-1")
                {
                    usmhMaterialId = Int32.Parse(((DropDownList)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("ddlUsmhMaterialEdit")).SelectedValue);
                    AssetSewerMHMaterialTypeGateway assetSewerMHMaterialTypeGatewayForUsmh = new AssetSewerMHMaterialTypeGateway();
                    assetSewerMHMaterialTypeGatewayForUsmh.LoadByMaterialId((int)usmhMaterialId, companyId);
                    usmhMaterial = assetSewerMHMaterialTypeGatewayForUsmh.GetMaterialType((int)usmhMaterialId);
                }

                string dsmhStreet = ((TextBox)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("tbxDsmhStreetEdit")).Text.Trim();
                string dsmhLatitude = ((TextBox)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("tbxDsmhLatitudeEdit")).Text.Trim();
                string dsmhLongitude = ((TextBox)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("tbxDsmhLongitudeEdit")).Text.Trim();
                string dsmhShape = ((DropDownList)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("ddlDsmhShapeEdit")).SelectedValue;
                string dsmhLocation = ((DropDownList)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("ddlDsmhLocationEdit")).SelectedValue;
                int? dsmhConditionRating = null;
                if (((DropDownList)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("ddlDsmhConditionRatingEdit")).SelectedValue != "-1")
                {
                    dsmhConditionRating = Int32.Parse(((DropDownList)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("ddlDsmhConditionRatingEdit")).SelectedValue);
                }
                int? dsmhMaterialId = null;
                string dsmhMaterial = "";
                if (((DropDownList)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("ddlDsmhMaterialEdit")).SelectedValue != "-1")
                {
                    dsmhMaterialId = Int32.Parse(((DropDownList)grdProjectAddSectionsNew.Rows[e.RowIndex].Cells[1].FindControl("ddlDsmhMaterialEdit")).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 = "";

                // Update datasets
                ProjectAddSectionsTDS dataSet = new ProjectAddSectionsTDS();
                dataSet.ProjectAddSectionsNew.Merge(projectAddSectionsNew, true);
                ProjectAddSectionsNew model = new ProjectAddSectionsNew(dataSet);

                // Update
                model.Update(id, sectionId, street, usmh, dsmh, cbxJL, cbxRA, cbxFLL, cbxPR, false, sectionId, 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);
                Session["projectAddSectionsNew"] = dataSet.ProjectAddSectionsNew;
                projectAddSectionsNew = dataSet.ProjectAddSectionsNew;
            }
            else
            {
                e.Cancel = true;
            }
        }
        /// <summary>
        /// Save a section
        /// </summary>
        /// <param name="row">row</param>
        /// <param name="projectId">projectId</param>
        /// <param name="countryId">countryId</param>
        /// <param name="provinceId">provinceId</param>
        /// <param name="countyId">countyId</param>
        /// <param name="cityId">cityId</param>
        /// <param name="companyId">companyId</param>
        /// <returns>section_assetId</returns>
        private int SaveSection(ProjectAddSectionsTDS.ProjectAddSectionsTempRow row, int projectId, Int64? countryId, Int64? provinceId, Int64? countyId, Int64? cityId, int companyId)
        {
            string street = ""; if (!row.IsStreetNull()) street = row.Street;
            string usmh = ""; if (!row.IsUSMHNull()) usmh = row.USMH;
            string dsmh = ""; if (!row.IsDSMHNull()) dsmh = row.DSMH;

            string usmhStreet = ""; if (!row.IsUsmhStreetNull()) usmhStreet = row.UsmhStreet;
            string usmhLatitude = ""; if (!row.IsUsmhLatitudeNull()) usmhLatitude = row.UsmhLatitude;
            string usmhLongitude = ""; if (!row.IsUsmhLongitudeNull()) usmhLongitude = row.UsmhLongitude;
            string usmhShape = ""; if (!row.IsUsmhShapeNull()) usmhShape = row.UsmhShape;
            string usmhLocation = ""; if (!row.IsUsmhLocationNull()) usmhLocation = row.UsmhLocation;
            int? usmhConditionRating = null; if (!row.IsUsmhConditionRatingNull()) usmhConditionRating = row.UsmhConditionRating;
            int? usmhMaterialId = null; if (!row.IsUsmhMaterialIdNull()) usmhMaterialId = row.UsmhMaterialId;
            string usmhTopDiameter = ""; if (!row.IsUsmhTopDiameterNull()) usmhTopDiameter = row.UsmhTopDiameter;
            string usmhTopDepth = ""; if (!row.IsUsmhTopDepthNull()) usmhTopDepth = row.UsmhTopDepth;
            string usmhDownDiameter = ""; if (!row.IsUsmhDownDiameterNull()) usmhDownDiameter = row.UsmhDownDiameter;
            string usmhDownDepth = ""; if (!row.IsUsmhDownDepthNull()) usmhDownDepth = row.UsmhDownDepth;
            int? usmhManholeRugs = null; if (!row.IsUsmhManholeRugsNull()) usmhManholeRugs = row.UsmhManholeRugs;
            string usmhRectangle1LongSide = ""; if (!row.IsUsmhRectangle1LongSideNull()) usmhRectangle1LongSide = row.UsmhRectangle1LongSide;
            string usmhRectangle1ShortSide = ""; if (!row.IsUsmhRectangle1ShortSideNull()) usmhRectangle1ShortSide = row.UsmhRectangle1ShortSide;
            string usmhRectangle2LongSide = ""; if (!row.IsUsmhRectangle2LongSideNull()) usmhRectangle2LongSide = row.UsmhRectangle2LongSide;
            string usmhRectangle2ShortSide = ""; if (!row.IsUsmhRectangle2ShortSideNull()) usmhRectangle2ShortSide = row.UsmhRectangle2ShortSide;
            string usmhTotalSurfaceArea = ""; if (!row.IsUsmhTotalSurfaceAreaNull()) usmhTotalSurfaceArea = row.UsmhTotalSurfaceArea;

            string dsmhStreet = ""; if (!row.IsDsmhStreetNull()) dsmhStreet = row.DsmhStreet;
            string dsmhLatitude = ""; if (!row.IsDsmhLatitudeNull()) dsmhLatitude = row.DsmhLatitude;
            string dsmhLongitude = ""; if (!row.IsDsmhLongitudeNull()) dsmhLongitude = row.DsmhLongitude;
            string dsmhShape = ""; if (!row.IsDsmhShapeNull()) dsmhShape = row.DsmhShape;
            string dsmhLocation = ""; if (!row.IsDsmhLocationNull()) dsmhLocation = row.DsmhLocation;
            int? dsmhConditionRating = null; if (!row.IsDsmhConditionRatingNull()) dsmhConditionRating = row.DsmhConditionRating;
            int? dsmhMaterialId = null; if (!row.IsDsmhMaterialIdNull()) dsmhMaterialId = row.DsmhMaterialId;
            string dsmhTopDiameter = ""; if (!row.IsDsmhTopDiameterNull()) dsmhTopDiameter = row.DsmhTopDiameter;
            string dsmhTopDepth = ""; if (!row.IsDsmhTopDepthNull()) dsmhTopDepth = row.DsmhTopDepth;
            string dsmhDownDiameter = ""; if (!row.IsDsmhDownDiameterNull()) dsmhDownDiameter = row.DsmhDownDiameter;
            string dsmhDownDepth = ""; if (!row.IsDsmhDownDepthNull()) dsmhDownDepth = row.DsmhDownDepth;
            int? dsmhManholeRugs = null; if (!row.IsDsmhManholeRugsNull()) dsmhManholeRugs = row.DsmhManholeRugs;
            string dsmhRectangle1LongSide = ""; if (!row.IsDsmhRectangle1LongSideNull()) dsmhRectangle1LongSide = row.DsmhRectangle1LongSide;
            string dsmhRectangle1ShortSide = ""; if (!row.IsDsmhRectangle1ShortSideNull()) dsmhRectangle1ShortSide = row.DsmhRectangle1ShortSide;
            string dsmhRectangle2LongSide = ""; if (!row.IsDsmhRectangle2LongSideNull()) dsmhRectangle2LongSide = row.DsmhRectangle2LongSide;
            string dsmhRectangle2ShortSide = ""; if (!row.IsDsmhRectangle2ShortSideNull()) dsmhRectangle2ShortSide = row.DsmhRectangle2ShortSide;
            string dsmhTotalSurfaceArea = ""; if (!row.IsDsmhTotalSurfaceAreaNull()) dsmhTotalSurfaceArea = row.DsmhTotalSurfaceArea;

            // insert usmh (if not exists)
            int? usmh_assetId = null;
            if (usmh != "")
            {
                LfsAssetSewerMH lfsAssetSewerUsmh = new LfsAssetSewerMH(null);
                usmh_assetId = lfsAssetSewerUsmh.InsertDirect(countryId, provinceId, countyId, cityId, usmh, usmhLatitude, usmhLongitude, usmhStreet, false, companyId, usmhShape, usmhLocation, usmhMaterialId, usmhTopDiameter, usmhTopDepth, "", "", "", usmhDownDiameter, usmhDownDepth, "", "", "", usmhRectangle1LongSide, usmhRectangle1ShortSide, usmhRectangle2LongSide, usmhRectangle2ShortSide, "", "", usmhManholeRugs, "", usmhTotalSurfaceArea, usmhConditionRating);

                // insert in LFS tables (only if not exists)
                AssetSewerMHGateway assetSewerMHGateway = new AssetSewerMHGateway();

                if (!assetSewerMHGateway.IsUsedInMHProject(projectId, usmh_assetId.Value))
                {
                    AssetSewerMHGateway assetSewerMhGatewayForUSMH = new AssetSewerMHGateway(null);
                    assetSewerMhGatewayForUSMH.InsertMHProject(usmh_assetId.Value, projectId, DateTime.Now, false, companyId);
                }

                if (row.MHRehabUsmh)
                {
                    //TODO MH
                    SaveMHRehabWork(projectId, usmh_assetId.Value, companyId);
                }
            }

            // insert dsmh (if not exists)
            int? dsmh_assetId = null;
            if (dsmh != "")
            {
                LfsAssetSewerMH lfsAssetSewerDsmh = new LfsAssetSewerMH(null);
                dsmh_assetId = lfsAssetSewerDsmh.InsertDirect(countryId, provinceId, countyId, cityId, dsmh, dsmhLatitude, dsmhLongitude, dsmhStreet, false, companyId, dsmhShape, dsmhLocation, dsmhMaterialId, dsmhTopDiameter, dsmhTopDepth, "", "", "", dsmhDownDiameter, dsmhDownDepth, "", "", "", dsmhRectangle1LongSide, dsmhRectangle1ShortSide, dsmhRectangle2LongSide, dsmhRectangle2ShortSide, "", "", dsmhManholeRugs, "", dsmhTotalSurfaceArea, dsmhConditionRating);

                // insert in LFS tables (only if not exists)
                AssetSewerMHGateway assetSewerMHGateway = new AssetSewerMHGateway();

                if (!assetSewerMHGateway.IsUsedInMHProject(projectId, dsmh_assetId.Value))
                {
                    AssetSewerMHGateway assetSewerMhGatewayForDSMH = new AssetSewerMHGateway(null);
                    assetSewerMhGatewayForDSMH.InsertMHProject(dsmh_assetId.Value, projectId, DateTime.Now, false, companyId);
                }

                if (row.MHRehabDsmh)
                {
                    //TODO MH
                    SaveMHRehabWork(projectId, dsmh_assetId.Value, companyId);
                }
            }

            // insert section
            LfsAssetSewerSection lfsAssetSewerSection = new LfsAssetSewerSection(null);
            string mapSize = ""; if (!row.IsMapSizeNull()) mapSize = row.MapSize;
            string mapLength = ""; if (!row.IsMapLengthNull()) mapLength = row.MapLength;

            int sectionMaterialRefId = 1; // first material when adding new section
            string sectionMaterial = ""; if (!row.IsSectionMaterialNull()) sectionMaterial = row.SectionMaterial;
            DateTime sectionMaterialDate = DateTime.Now;

            int section_assetId = lfsAssetSewerSection.InsertDirect(countryId, provinceId, countyId, cityId, row.SectionID, street, usmh_assetId, dsmh_assetId, mapSize, "", mapLength, "", null, null, "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", false, companyId, "", "", sectionMaterialRefId, sectionMaterial, sectionMaterialDate);

            return section_assetId;
        }