Select to view content in your preferred language

Create and Write Data to a Feature Class (add Records)

1916
6
04-05-2010 10:16 PM
ScottAndreasen
Emerging Contributor
I'm new to C#, developing and programming in general but I'm trying to build a form that will write to a new (initially blank) point feature class file and add records (or rows) with the data in the form. The SHAPE info will come from another feature class existing in the table of contents.

I'm having trouble adding records with data. In this case, my form is not fully developed and will simply be adding data to the 'Team' field and to the SHAPE field. But, I think something is not right with the following code:

            //Update attributes using form controls
            IFeature pNewFeat = pTeamInfo.FeatureClass.CreateFeature(); 
            pNewFeat.Shape = pFeatRow.ShapeCopy;
            pNewFeat.set_Value(pFeatRow.Fields.FindField("Team"), txtTeam.Text);
            pNewFeat.Store();

Note, the entire frm Code is included below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ArcMapUI;
using System.Security.Principal;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.Framework;

namespace v1._02
{
    public partial class frmPickPeak : Form
    {
        private IMxDocument pMxDoc;
        private IMap pMap;
        private IFeatureLayer pPeakInfo;
        private IFeatureLayer pTeamInfo;
        private IFeature pFeatRow;
       
        public frmPickPeak()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmEditer_Load);
            btnRegister.Click += new EventHandler(btnRegister_Click);
            btnExit.Click += new EventHandler(btnExit_Click);
        }

        private void frmEditer_Load(object sender, EventArgs e)
        {
            //get MxDoc from Application, get Map from MxDoc
            pMxDoc = cmdOnTarget.g_application.Document as IMxDocument;
            pMap = pMxDoc.FocusMap;

            //get cursor of selected row from FeatureSelection SelectionSet
            pPeakInfo = cmdOnTarget.getLayerByToc("PeakInfo");
            pTeamInfo = cmdOnTarget.getLayerByToc("TeamInfo");
            IFeatureSelection pFeatSel = pPeakInfo as IFeatureSelection;
           
            //Get row from Cursor
            pFeatRow = pPeakInfo.FeatureClass.GetFeature(pFeatSel.SelectionSet.IDs.Next());

            //Update form controls using row attributes
            lblPeak.Text = pFeatRow.get_Value(pFeatRow.Fields.FindField("Name")).ToString();
            lblLat.Text = pFeatRow.get_Value(pFeatRow.Fields.FindField("Lat")).ToString();
            lblLong.Text = pFeatRow.get_Value(pFeatRow.Fields.FindField("Long")).ToString();
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            //Change mouse cursor to hourglass
            IMouseCursor pMouseCur = new MouseCursor();
            pMouseCur.SetCursor(2);

            //Start Editing
            IDataset pDataset = pPeakInfo as IDataset;
            IEditor pEditor = cmdOnTarget.g_application.FindExtensionByName("ESRI Object Editor") as IEditor;

            if (pEditor.EditState != esriEditState.esriStateEditing)
            {
                pEditor.StartEditing(pDataset.Workspace);
            }

            //Update attributes using form controls
            pFeatRow.set_Value(pFeatRow.Fields.FindField("Picked"), "yes");
            pFeatRow.Store();

            //Stop editing and save edits
            pEditor.StopEditing(true);

            //Edit the feature class entitled: TeamInfo

            IDataset pDataset2 = pTeamInfo as IDataset;
            IEditor pEditor2 = cmdOnTarget.g_application.FindExtensionByName("ESRI Object Editor") as IEditor;

            if (pEditor2.EditState != esriEditState.esriStateEditing)
            {
                pEditor2.StartEditing(pDataset2.Workspace);
            }

            //Update attributes using form controls
            IFeature pNewFeat = pTeamInfo.FeatureClass.CreateFeature(); 
            pNewFeat.Shape = pFeatRow.ShapeCopy;
            pNewFeat.set_Value(pFeatRow.Fields.FindField("Team"), txtTeam.Text);
            pNewFeat.Store();

            pEditor.StopEditing(true);

            this.Dispose(); //Dispose (exit) form
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}


I'm not really sure what the problem is. A new record is being written, but the Team field is not being updated. Likewise, copies of the new points do not show up in ArcMap so I doubt the SHAPE info is copying. Please help, and I'll try to clarify anything if this thread is not clear.

Thanks
Scott
0 Kudos
6 Replies
RobChasan
Regular Contributor
Try getting rid of the blank space in the string "Team ".
0 Kudos
ScottAndreasen
Emerging Contributor
I don't have a space in Team in my code. That somehow showed up when I copied and pasted the code. Everything builds fine and runs. The problem is that nothing is written to the new feature class.
0 Kudos
RobChasan
Regular Contributor
Another thought:  After the line  "pNewFeat.Store();",

consider changing "pEditor.StopEditing(true);"  to "pEditor2.StopEditing(true);"
0 Kudos
ScottAndreasen
Emerging Contributor
That didn't solve my problem. But your comment did help me make my program more efficient.

I am currently thinking that this has something to do with pNewFeat and setting the cursor. I called on it while editing the first feature class and I also called it while editing the second feature class without any modification. I can't quite figure out how/if  the cursor information is incorporated when creating a new feature.
0 Kudos
ScottAndreasen
Emerging Contributor
This is the way my frm code looks now:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ArcMapUI;
using System.Security.Principal;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.Geometry;
namespace v1._02
{
    public partial class frmPickPeak : Form
    {
        private IMxDocument pMxDoc;
        private IMap pMap;
        private IFeatureLayer pPeakInfo;
        private IFeatureLayer pTeamInfo;
        private IFeature pFeatRow;
      
        public frmPickPeak()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmEditer_Load);
            btnRegister.Click += new EventHandler(btnRegister_Click);
            btnExit.Click += new EventHandler(btnExit_Click);
        }
        private void frmEditer_Load(object sender, EventArgs e)
        {
            //get MxDoc from Application, get Map from MxDoc
            pMxDoc = cmdOnTarget.g_application.Document as IMxDocument;
            pMap = pMxDoc.FocusMap;
            //get cursor of selected row from FeatureSelection SelectionSet
            pPeakInfo = cmdOnTarget.getLayerByToc("PeakInfo");
            pTeamInfo = cmdOnTarget.getLayerByToc("TeamInfo");
            IFeatureSelection pFeatSel = pPeakInfo as IFeatureSelection;
          
            //Get row from Cursor
            pFeatRow = pPeakInfo.FeatureClass.GetFeature(pFeatSel.SelectionSet.IDs.Next());

            //Update form controls using row attributes
            lblPeak.Text = pFeatRow.get_Value(pFeatRow.Fields.FindField("Name")).ToString();
            lblLat.Text = pFeatRow.get_Value(pFeatRow.Fields.FindField("Lat")).ToString();
            lblLong.Text = pFeatRow.get_Value(pFeatRow.Fields.FindField("Long")).ToString();
        }
        private void btnRegister_Click(object sender, EventArgs e)
        {
            //Start Editing
            IDataset pDataset = pPeakInfo as IDataset;
            IEditor pEditor = cmdOnTarget.g_application.FindExtensionByName("ESRI Object Editor") as IEditor;
          
            if (pEditor.EditState != esriEditState.esriStateEditing)
            {
                pEditor.StartEditing(pDataset.Workspace);
            }
            //Update attributes using form controls
            pFeatRow.set_Value(pFeatRow.Fields.FindField("Picked"), "yes");
            pFeatRow.Store();
            //Edit the feature class entitled: TeamInfo
            IDataset pDataset2 = pTeamInfo as IDataset;
          
            IFeature pNewFeat = pTeamInfo.FeatureClass.CreateFeature();
            pNewFeat.Shape = pFeatRow.ShapeCopy;
            string tmNum = txtTeam.Text;
            pNewFeat.set_Value(pFeatRow.Fields.FindField("Team"), tmNum);
            pNewFeat.Store();

            pEditor.StopEditing(true);
          
            //Dispose (exit) form
            this.Dispose();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}

I think there is something suspect about the bold lines. But I need some help.

Scott
0 Kudos
ScottAndreasen
Emerging Contributor
A friend helped me figure this out. -Thanks Jon.

Instead of:

pNewFeat.set_Value(pFeatRow.Fields.FindField("Team "), tmNum);

I needed to use:

pNewFeat.set_Value(pNewFeat.Fields.FindField("Team"), tmNum);

above this, I declared pNewFeat but pointed it to old pFeatRow
0 Kudos