get all the values of particular field

3408
2
12-23-2014 08:44 AM
ababreddy
New Contributor

Hello, I want to develop a tool using Arc objects to get all the values of particular field. below is code but i am getting an error as null reference. Can anyone help me how to solve this.

public override void OnClick()

        {

 

            IMxDocument pmx = m_application.Document as IMxDocument;

            IMap pmap = pmx.FocusMap;

 

            //ILayer player = pmap.get_Layer(0);

 

            IFeatureLayer pflayer = pmap.get_Layer(0) as IFeatureLayer;

 

            IFeatureClass fc = pflayer.FeatureClass;

            IFields pfield = fc as IFields;

            ITable ptable = fc as ITable;

            ICursor pcursor = ptable.Search(null, false);

            int fldindex = fc.Fields.FindField("NAME");

            IRow prow = pcursor.NextRow();

 

            if (prow != null)

            {

 

                string output = " ";

                for (int i = 0; i <= pfield.FieldCount - 1; i++)

                {

 

                    output += pfield.get_Field(i).Name + ": " + prow.get_Value(i).ToString() + " \n";

                    MessageBox.Show(output);

                    prow = pcursor.NextRow();

                }

 

            }

                                              

        }

Tags (1)
0 Kudos
2 Replies
FreddieGibson
Occasional Contributor III

Are you needing to write this within an addin and if you debug this can you verify that you're pulling the first layer from the map?

Also, what is your end goal? Are you trying to get all of the unique values in a field? If so, have you tried using IDataStatistics?

0 Kudos
DuncanHornby
MVP Notable Contributor

I would imagine the problem is with this line?

output += pfield.get_Field(i).Name + ": " + prow.get_Value(i).ToString() + " \n";

Your input is a FeatureLayer so assuming its a shapefile the first field is FID and the second is the Shape field. You seem to be stepping through the fields of each row getting the field name and its value. I guess you are trying to convert a geometry into a string, I suspect that was not your intention? You need to test each field type and skip the geometry field.

Also you create a variable fldindex then do nothing with it...

0 Kudos