Change attribute table values of shape file.

2464
8
04-28-2011 09:48 PM
Highlighted
Occasional Contributor
Hello everyone!
I want to change attribute table values of shape file using C#.
I tried with code below. The code works, but values are not change.

            IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
            IWorkspace wrksp = workspaceFactory.OpenFromFile(@"D:\Shape\", 0);
            IFeatureWorkspace pFeatWorkspace = wrksp as IFeatureWorkspace;                                                      
            ITable myTable = pFeatWorkspace.OpenTable("Shape1");
            IWorkspaceEdit wrkspcedit = pFeatWorkspace as IWorkspaceEdit;                     
            if (wrkspcedit.IsBeingEdited())
            {           
                wrkspcedit.StartEditing(false);
                wrkspcedit.StartEditOperation();
            }           
            myTable.GetRow(5).set_Value(8,"anyvalue");          
            wrkspcedit.StopEditOperation();
            wrkspcedit.StopEditing(true);

Can someone help me?
Thanks!
Reply
0 Kudos
8 Replies
Highlighted
New Contributor
It looks like you are not starting the edit session. Your conditional only executes if the workspace is being edited...
Reply
0 Kudos
Highlighted
Occasional Contributor
Thanks for your reply.
I tried the code without if(). But, the attribute table value did not change.
I have 2 shapes in directory "D:\Shape\" .  Shape1 and Shape2.
I need change values of Shape1.
IWorkspaceEdit wrkspcedit = pFeatWorkspace as IWorkspaceEdit;
But here wrkspcedit don't show Shape1.
I tried the code below, but have an error.(NullReferenceException was unhandled)

WorkspaceEdit wrkspcedit = myTable as IWorkspaceEdit;
wrkspcedit.StartEditing(false); // NullReferenceException was unhandled
wrkspcedit.StartEditOperation();
myTable.GetRow(5).set_Value(8,"anyvalue");
wrkspcedit.StopEditOperation();
wrkspcedit.StopEditing(true);

What can I do?
Reply
0 Kudos
Highlighted
New Contributor
I am experiencing the same problem with shapefiles - if I try to update any attribute values inside an edit session then the changes seem not to be persisted.
The same code works fine if the feature class/table is in a file geodatabase.
And if I make the update to the shapefile directly without enclosing it in an IWorkspaceEdit Start/Stop session then it also seems to work OK.

SO should we not use IWorkspaceEdit with shapefiles?
Reply
0 Kudos
Highlighted
New Contributor III
Any other suggestion for shapefiles?

Maybe any SP?

Sincerely
Ezequias Rocha
Reply
0 Kudos
Highlighted
Occasional Contributor
Hi,

I have suggestions 🙂 !

try doing this one by one.

1. For shapefile first try without doing startediting and stopediting.
2. trying using same code without starteditoperation and stopeditoperation coz this is used for SDE.
3. try using IEditor instead of workspaceedit.
4. trying using Ifeatureclass.search instead of update.

something outta this should work!

Regards,
Sameer
Reply
0 Kudos
Highlighted
Occasional Contributor
Thanks for your reply.
I tried the code without if(). But, the attribute table value did not change.
I have 2 shapes in directory "D:\Shape\" .  Shape1 and Shape2.
I need change values of Shape1.
IWorkspaceEdit wrkspcedit = pFeatWorkspace as IWorkspaceEdit;
But here wrkspcedit don't show Shape1.
I tried the code below, but have an error.(NullReferenceException was unhandled)

WorkspaceEdit wrkspcedit = myTable as IWorkspaceEdit;
wrkspcedit.StartEditing(false); // NullReferenceException was unhandled
wrkspcedit.StartEditOperation();
myTable.GetRow(5).set_Value(8,"anyvalue");
wrkspcedit.StopEditOperation();
wrkspcedit.StopEditing(true);

What can I do?


Hi,


Found out!
Please read this
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//00250000089m000000

you havent called store() on to this row to commit changes to the shapefile.

do it this way replace myTable.GetRow(5).set_Value(8,"anyvalue"); with

IRow pRow = myTable.GetRow(5);
pRow.set_Value(8,"anyvalue");
pRow.store();

and this will work!

Regards,
Sameer Puppal
Reply
0 Kudos
Highlighted
New Contributor III
Hi,

I have suggestions 🙂 !

try doing this one by one.

1. For shapefile first try without doing startediting and stopediting.
2. trying using same code without starteditoperation and stopeditoperation coz this is used for SDE.
3. try using IEditor instead of workspaceedit.
4. trying using Ifeatureclass.search instead of update.

something outta this should work!

Regards,
Sameer


Sammer

I am trying to make an insert. I am outside the ArcMap environment and this alternatives where tryed,but 3 and 4 (thanks). I also tryed a RowBuffer. Just in case. I am trying to update a dbf file, not a shapefile.

Any other suggestion? What such strange error message.

Regards
Ezequias
Reply
0 Kudos
Highlighted
New Contributor III
Sammer

I am trying to make an insert. I am outside the ArcMap environment and this alternatives where tryed,but 3 and 4 (thanks). I also tryed a RowBuffer. Just in case. I am trying to update a dbf file, not a shapefile.

Any other suggestion? What such strange error message.

Regards
Ezequias


I found the problem. I simply set the "value" of set_value method to a variable and BINGO.

Quite strange. I think it is a bug.

Regards
Ezequias
Reply
0 Kudos