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);
              }
        }