protected override void OnClick()
        {
            m_pApp = (IApplication)ArcMap.Application;
              if (m_pApp == null)
            //if the app is null then could be running from ArcCatalog
            m_pApp = (IApplication)ArcCatalog.Application;

              if (m_pApp == null)
              {
            MessageBox.Show("Could not access the application.", "No Application found");
            return;
              }
              IGxApplication pGXApp = (IGxApplication)m_pApp;
              stdole.IUnknown pUnk = null;
              try
              {
            pUnk = (stdole.IUnknown)pGXApp.SelectedObject.InternalObjectName.Open();
              }
              catch (COMException ex)
              {
            if (ex.ErrorCode == (int)fdoError.FDO_E_DATASET_TYPE_NOT_SUPPORTED_IN_RELEASE ||
            ex.ErrorCode == -2147220944)
              MessageBox.Show("The dataset is not supported in this release.", "Could not open the dataset");
            else
              MessageBox.Show(ex.ErrorCode.ToString(), "Could not open the dataset");
            return;
              }

              if (pUnk is ICadastralFabric)
            m_pCadaFab = (ICadastralFabric)pUnk;
              else
              {
            MessageBox.Show("Please select a parcel fabric and try again.", "Not a parcel fabric");
            return;
              }

              IMouseCursor pMouseCursor = new MouseCursorClass();
              pMouseCursor.SetCursor(2);

              Utils FabricUTILS = new Utils();

              ITable pTable = m_pCadaFab.get_CadastralTable(esriCadastralFabricTable.esriCFTParcels);
              IDataset pDS = (IDataset)pTable;
              IWorkspace pWS = pDS.Workspace;
              bool bIsFileBasedGDB = true;
              bool bIsUnVersioned = true;

              FabricUTILS.GetFabricPlatform(pWS, m_pCadaFab, out bIsFileBasedGDB,
            out bIsUnVersioned);

              //Do a Start and Stop editing to make sure we're not running in an edit session
              if (!FabricUTILS.StartEditing(pWS, true))
              {//if start editing fails then bail
            if (pUnk != null)
              Marshal.ReleaseComObject(pUnk);
            Cleanup(pMouseCursor, null, pTable, null, pWS, null, true);
            FabricUTILS = null;
            return;
              }
              FabricUTILS.StopEditing(pWS);

              ITable pPlansTable = m_pCadaFab.get_CadastralTable(esriCadastralFabricTable.esriCFTPlans);
              ITable pParcelsTable = m_pCadaFab.get_CadastralTable(esriCadastralFabricTable.esriCFTParcels);

              m_pProgressorDialogFact = new ProgressDialogFactoryClass();
              m_pTrackCancel = new CancelTrackerClass();
              m_pStepProgressor = m_pProgressorDialogFact.Create(m_pTrackCancel, m_pApp.hWnd);
              IProgressDialog2 pProgressorDialog = (IProgressDialog2)m_pStepProgressor;
              int iRowCount = pPlansTable.RowCount(null);
              int iRowCount2 = pParcelsTable.RowCount(null);
              m_pStepProgressor.MinRange = 1;
              m_pStepProgressor.MaxRange =iRowCount  + iRowCount2;
              m_pStepProgressor.StepValue = 1;
              pProgressorDialog.Animation = ESRI.ArcGIS.Framework.esriProgressAnimationTypes.esriProgressSpiral;
              pProgressorDialog.ShowDialog();
              m_pStepProgressor.Message = "Searching " + iRowCount2.ToString() + " parcel records...";

              int iFixCnt = 0;
              //load all the plan names into a string array

              ArrayList NoPlanParcels = new ArrayList();
              ArrayList NoPlanParcelsGrouped = new ArrayList();
              ArrayList PlansList = new ArrayList();
              Dictionary<int, int> ParcelLookup = new Dictionary<int, int>();

              if (!FindNoPlanParcels(pPlansTable, pParcelsTable, ref PlansList, ref NoPlanParcels,
            ref NoPlanParcelsGrouped, ref ParcelLookup, out iFixCnt))
              {
            pProgressorDialog.HideDialog();
            if (iFixCnt > 0)
              MessageBox.Show("Canceled searching for parcels with no plan.", "Fix Parcels With No Name");

            NoPlanParcels = null;

            Cleanup(pMouseCursor, null, pTable, pPlansTable, pWS, pProgressorDialog, true);
            return;
              }

              m_pStepProgressor.Message = "Search complete.";

              NoPlanParcels.Sort();

              dlgFixParcelsWithNoPlan MissingPlansDialog = new dlgFixParcelsWithNoPlan();
              FillTheList(MissingPlansDialog.listView1, NoPlanParcels);
              FillTheList(MissingPlansDialog.listViewByGroup, NoPlanParcelsGrouped);
              FillTheListBox(MissingPlansDialog.listPlans, PlansList);

              MissingPlansDialog.ThePlansList = PlansList;

              MissingPlansDialog.label1.Text = "Found " + iFixCnt.ToString() +
            " parcels that have a missing plan record.";
              MissingPlansDialog.lblSelectionCount.Text = "(" + iFixCnt.ToString() + " of "
            + iFixCnt.ToString() + " selected to fix)";

              //cleanup
              Cleanup(null, null, null, null, null, pProgressorDialog, false);

              DialogResult dResult = MissingPlansDialog.ShowDialog();

              if (dResult == DialogResult.Cancel)
              {
            Cleanup(null, null, pTable, pPlansTable, pWS, pProgressorDialog, true);
            return;
              }

              //re-initilize the progressor
              m_pProgressorDialogFact = new ProgressDialogFactoryClass();
              m_pTrackCancel = new CancelTrackerClass();
              m_pStepProgressor = m_pProgressorDialogFact.Create(m_pTrackCancel, m_pApp.hWnd);
              pProgressorDialog = (IProgressDialog2)m_pStepProgressor;

              m_pStepProgressor.MinRange = 1;
              m_pStepProgressor.MaxRange = MissingPlansDialog.listView1.CheckedItems.Count;
              m_pStepProgressor.StepValue = 1;
              pProgressorDialog.Animation = ESRI.ArcGIS.Framework.esriProgressAnimationTypes.esriProgressSpiral;
              pProgressorDialog.ShowDialog();
              m_pStepProgressor.Message = "Fixing parcels without a plan...";

              if (!FabricUTILS.StartEditing(pWS, bIsUnVersioned))
              {
            pProgressorDialog.HideDialog();
            Cleanup(null, null, pTable, pPlansTable, pWS, pProgressorDialog, true);
            FabricUTILS = null;
            return;
              }
              int iNewPlanID = 0;

              //Need to collect the choices from the UI and write the results to the DB
              ICadastralFabricSchemaEdit2 pSchemaEd = (ICadastralFabricSchemaEdit2)m_pCadaFab;

              if (MissingPlansDialog.radioBtnExistingPlan.Checked)
              {//Get the id of the EXISTING Plan
            string sPlanString = MissingPlansDialog.listPlans.SelectedItem.ToString();
            string[] sPlanOID = Regex.Split(sPlanString, "oid:");
            sPlanOID[1].Trim();
            iNewPlanID = Convert.ToInt32(sPlanOID[1].Remove(sPlanOID[1].LastIndexOf(")")));
              }

              Dictionary<string, int> PlanLookUp= new Dictionary<string,int>();
              ArrayList iUnitsAndFormat = new ArrayList();
              iUnitsAndFormat.Add(m_AngleUnits);
              iUnitsAndFormat.Add(m_AreaUnits);
              iUnitsAndFormat.Add(m_DistanceUnits);
              iUnitsAndFormat.Add(m_DirectionFormat);
              iUnitsAndFormat.Add(m_LineParams);

              if (MissingPlansDialog.radioBtnUserDef.Checked)
              { //create a NEW plan named with user's entered text
            ArrayList sPlanInserts = new ArrayList();
            pSchemaEd.ReleaseReadOnlyFields(pPlansTable, esriCadastralFabricTable.esriCFTPlans); //release safety-catch
            sPlanInserts.Add(MissingPlansDialog.txtPlanName.Text);
            FabricUTILS.InsertPlanRecords(pPlansTable, sPlanInserts,iUnitsAndFormat,PlansList, bIsUnVersioned,
              null, null, ref PlanLookUp);
            if (!PlanLookUp.TryGetValue(MissingPlansDialog.txtPlanName.Text, out iNewPlanID))
            {
              Cleanup(null, null, pTable, pPlansTable, pWS, pProgressorDialog, true);
              FabricUTILS = null;
              return;
            }
              }

              if (MissingPlansDialog.radioBtnPlanID.Checked)
              {//create multiple new plans for each PlanID
            ArrayList sPlanInserts = new ArrayList();
            foreach (ListViewItem listItem in MissingPlansDialog.listViewByGroup.CheckedItems)
              sPlanInserts.Add("[" + listItem.SubItems[0].Text + "]");

            pSchemaEd.ReleaseReadOnlyFields(pPlansTable, esriCadastralFabricTable.esriCFTPlans); //release safety-catch
            FabricUTILS.InsertPlanRecords(pPlansTable, sPlanInserts, iUnitsAndFormat, PlansList, bIsUnVersioned,
              null, null, ref PlanLookUp);
              }

              ArrayList sParcelUpdates = new ArrayList();
              sParcelUpdates.Add("");
              int i = 0;
              int iCnt = 0;
              int iTokenLimit = 995;
              foreach (ListViewItem listItem in MissingPlansDialog.listView1.CheckedItems)
              {
            string s = listItem.SubItems[1].Text;
            string[] sItems = Regex.Split(s, "id:");
            if (iCnt >= iTokenLimit)//time to start a new row
            {
              sParcelUpdates.Add("");//add a new item to the arraylist
              iCnt = 0;//reset token counter
              i++;//increment array index
            }
            sItems[1] = sItems[1].Remove(sItems[1].LastIndexOf(")"));

            if (iCnt == 0)
              sParcelUpdates[i] += sItems[1];
            else
              sParcelUpdates[i] += "," + sItems[1];
            iCnt++;
              }

              //============edit block==========
              try
              {
            pSchemaEd.ReleaseReadOnlyFields(pTable, esriCadastralFabricTable.esriCFTParcels); //release safety-catch

            if (MissingPlansDialog.radioBtnUserDef.Checked || MissingPlansDialog.radioBtnExistingPlan.Checked)
            {
              if (!FabricUTILS.UpdateParcelRecords(pTable, sParcelUpdates, iNewPlanID, bIsUnVersioned,
            m_pStepProgressor, m_pTrackCancel))
              {
            pSchemaEd.ResetReadOnlyFields(esriCadastralFabricTable.esriCFTParcels);
            pSchemaEd.ResetReadOnlyFields(esriCadastralFabricTable.esriCFTPlans);
            FabricUTILS.AbortEditing(pWS);
            Cleanup(null, null, pTable, pPlansTable, pWS, pProgressorDialog, true);
            FabricUTILS = null;
            return;
              }
            }
            if (MissingPlansDialog.radioBtnPlanID.Checked)
            {
              if (!FabricUTILS.UpdateParcelRecordsByPlanGroup(pTable, sParcelUpdates, PlanLookUp,
            ParcelLookup, bIsUnVersioned, m_pStepProgressor, m_pTrackCancel))
              {
            pSchemaEd.ResetReadOnlyFields(esriCadastralFabricTable.esriCFTParcels);
            pSchemaEd.ResetReadOnlyFields(esriCadastralFabricTable.esriCFTPlans);
            FabricUTILS.AbortEditing(pWS);
            Cleanup(null, null, pTable, pPlansTable, pWS, pProgressorDialog, true);
            FabricUTILS = null;
            return;
              }
            }

            pSchemaEd.ResetReadOnlyFields(esriCadastralFabricTable.esriCFTParcels);
            pSchemaEd.ResetReadOnlyFields(esriCadastralFabricTable.esriCFTPlans);

            FabricUTILS.StopEditing(pWS);

            //Cleanup(null, null, pTable, pPlansTable, pWS, pProgressorDialog, true);
            //FabricUTILS = null;
              }
              catch (COMException Ex)
              {
            MessageBox.Show(Ex.ErrorCode.ToString() +":" + Ex.Message,"Fix Missing Plans");
              }
              finally
              {
            Cleanup(null, null, pTable, pPlansTable, pWS, pProgressorDialog, true);
            FabricUTILS = null;
              }
        }
        protected override void OnClick()
        {
            m_pApp = (IApplication)ArcMap.Application;
              if (m_pApp == null)
            //if the app is null then could be running from ArcCatalog
            m_pApp = (IApplication)ArcCatalog.Application;

              if (m_pApp == null)
              {
            MessageBox.Show("Could not access the application.", "No Application found");
            return;
              }
              IGxApplication pGXApp = (IGxApplication)m_pApp;
              stdole.IUnknown pUnk = null;
              try
              {
            pUnk = (stdole.IUnknown)pGXApp.SelectedObject.InternalObjectName.Open();
              }
              catch (COMException ex)
              {
            if (ex.ErrorCode == (int)fdoError.FDO_E_DATASET_TYPE_NOT_SUPPORTED_IN_RELEASE ||
            ex.ErrorCode == -2147220944)
              MessageBox.Show("The dataset is not supported in this release.", "Could not open the dataset");
            else
              MessageBox.Show(ex.ErrorCode.ToString(), "Could not open the dataset");
            return;
              }

              if (pUnk is ICadastralFabric)
            m_pCadaFab = (ICadastralFabric)pUnk;
              else
              {
            MessageBox.Show("Please select a parcel fabric and try again.", "Not a parcel fabric");
            return;
              }

              IMouseCursor pMouseCursor = new MouseCursorClass();
              pMouseCursor.SetCursor(2);

              Utils FabricUTILS = new Utils();

              ITable pTable = m_pCadaFab.get_CadastralTable(esriCadastralFabricTable.esriCFTParcels);
              IDataset pDS = (IDataset)pTable;
              IWorkspace pWS = pDS.Workspace;
              bool bIsFileBasedGDB = true;
              bool bIsUnVersioned = true;

              FabricUTILS.GetFabricPlatform(pWS, m_pCadaFab, out bIsFileBasedGDB,
            out bIsUnVersioned);

              //Do a Start and Stop editing to make sure we're not running in an edit session
              if (!FabricUTILS.StartEditing(pWS, true))
              {//if start editing fails then bail
            if (pUnk != null)
              Marshal.ReleaseComObject(pUnk);
            Cleanup(pMouseCursor, null, pTable, null, pWS, null);
            FabricUTILS = null;
            return;
              }
              FabricUTILS.StopEditing(pWS);
              IFIDSet pPlansToDelete = null;

              try
              {
            string[] SummaryNames = new string[0]; //define as dynamic array
            string[] RepeatPlans = new string[0]; //define as dynamic array
            ITable pPlansTable = m_pCadaFab.get_CadastralTable(esriCadastralFabricTable.esriCFTPlans);
            //load all the plan names into a string array
            m_pProgressorDialogFact = new ProgressDialogFactoryClass();
            m_pTrackCancel = new CancelTrackerClass();
            m_pStepProgressor = m_pProgressorDialogFact.Create(m_pTrackCancel, m_pApp.hWnd);
            IProgressDialog2 pProgressorDialog = (IProgressDialog2)m_pStepProgressor;
            int iRowCount = pPlansTable.RowCount(null);
            m_pStepProgressor.MinRange = 1;
            m_pStepProgressor.MaxRange = iRowCount * 2;
            m_pStepProgressor.StepValue = 1;
            pProgressorDialog.Animation = ESRI.ArcGIS.Framework.esriProgressAnimationTypes.esriProgressSpiral;
            pProgressorDialog.ShowDialog();
            m_pStepProgressor.Message = "Finding same-name plans to merge...";

            int iRepeatCnt = 0;

            if (!FindRepeatPlans(pPlansTable, out RepeatPlans, out SummaryNames, out iRepeatCnt))
            {
              pProgressorDialog.HideDialog();
              if (iRepeatCnt == 0)
              {
            MessageBox.Show("All plans in the fabric have unique names." +
               Environment.NewLine + "There are no plans to merge.", "Merge plans by name");
              }
              else
              {
            MessageBox.Show("There was a problem searching for repeat plans.", "Merge plans by name");
              }
              SummaryNames = null;
              RepeatPlans = null;
              Cleanup(pMouseCursor, null, pTable, pPlansTable, pWS, pProgressorDialog);
              return;
            }

            dlgMergeSameNamePlans TheSummaryDialog = new dlgMergeSameNamePlans();

            FillTheSummaryList(TheSummaryDialog, SummaryNames);

            DialogResult dResult = TheSummaryDialog.ShowDialog();

            if (dResult == DialogResult.Cancel)
            {
              pProgressorDialog.HideDialog();
              SummaryNames = null;
              RepeatPlans = null;
              Cleanup(pMouseCursor, null,pTable, pPlansTable, pWS, pProgressorDialog);
              pPlansTable = null;
              return;
            }

            //get the time now
            m_pStartTime = new TimeClass();
            m_pStartTime.SetFromCurrentLocalTime();

            Dictionary<int, int> Lookup = new Dictionary<int, int>();
            string[] InClause = new string[0]; //define as dynamic array

            m_pStepProgressor.Message = "Creating the merge query...";
            FabricUTILS.BuildSearchMapAndQuery(RepeatPlans, out Lookup, out InClause, out pPlansToDelete);

            ICadastralFabricSchemaEdit2 pSchemaEd = (ICadastralFabricSchemaEdit2)m_pCadaFab;
            ITable ParcelTable = m_pCadaFab.get_CadastralTable(esriCadastralFabricTable.esriCFTParcels);
            pSchemaEd.ReleaseReadOnlyFields(ParcelTable, esriCadastralFabricTable.esriCFTParcels); //release safety-catch

            if (!FabricUTILS.StartEditing(pWS, bIsUnVersioned))
            {
              Cleanup(pMouseCursor, pPlansToDelete,pTable, pPlansTable, pWS, pProgressorDialog);
              InClause = null;
              Lookup.Clear();
              Lookup = null;
              return;
            }

            //setup progressor dialog for merge
            m_pStepProgressor.Message = "Moving parcels from source to target plans...";

            if (!FabricUTILS.MergePlans(ParcelTable, Lookup, InClause, bIsUnVersioned,m_pStepProgressor,m_pTrackCancel))
            {
              FabricUTILS.AbortEditing(pWS);
              pSchemaEd.ResetReadOnlyFields(esriCadastralFabricTable.esriCFTParcels);
              Cleanup(pMouseCursor, pPlansToDelete,pTable,pPlansTable, pWS,pProgressorDialog);
              InClause = null;
              Lookup.Clear();
              Lookup = null;
              return;
            }

            if (TheSummaryDialog.checkBox1.Checked)
            {
              //setup progressor dialog for Delete
              m_pStepProgressor.MaxRange = pPlansToDelete.Count();
              m_pStepProgressor.Message = "Deleting source plans...";

              if (bIsUnVersioned)
              {
            if (!FabricUTILS.DeleteRowsUnversioned(pWS, pPlansTable, pPlansToDelete, m_pStepProgressor, m_pTrackCancel))
              Cleanup(pMouseCursor, pPlansToDelete,pTable,pPlansTable,pWS,pProgressorDialog);
              }
              else
              {
            if (!FabricUTILS.DeleteRowsByFIDSet(pPlansTable, pPlansToDelete, m_pStepProgressor, m_pTrackCancel))
              Cleanup(pMouseCursor, pPlansToDelete,pTable,pPlansTable, pWS,pProgressorDialog);
              }
            }

            pSchemaEd.ResetReadOnlyFields(esriCadastralFabricTable.esriCFTParcels);

            m_pEndTime = new TimeClass();
            m_pEndTime.SetFromCurrentLocalTime();
            ITimeDuration HowLong = m_pEndTime.SubtractTime(m_pStartTime);

            m_pStepProgressor.Message = "["
              + HowLong.Hours.ToString("00") + "h "
              + HowLong.Minutes.ToString("00") + "m "
              + HowLong.Seconds.ToString("00") + "s]" + "  Saving changes...please wait.";

            FabricUTILS.StopEditing(pWS);
            Cleanup(pMouseCursor, pPlansToDelete,pTable,pPlansTable, pWS,pProgressorDialog);

              }
              catch (COMException ex)
              {
            MessageBox.Show(Convert.ToString(ex.ErrorCode));
            Cleanup(pMouseCursor, pPlansToDelete,pTable,null,pWS,null);
              }
        }