Change the field value on layer from SQL

I loading a layer from PostgreSQL. How do I change the field value correctly? The layer is loaded in memory. I don’t need to change the values on the server. I used to do this for DBF files:

var GidrantsLayer: IShapefile = EzMap.Shapefile[GidrantsHandle];
var MwShapeId: integer;
var ShapeGidrant: IShape = GidrantsLayer.Shape[MwShapeId];
var EzTable: Table = GidrantsLayer.Table;
EzTable.EditCellValue(FieldIndexByName['ID_TYPE_PG'], MwShapeId, 4);
GidrantsLayer.StopEditingTable(True, cBack);

Now an exception is called. I’m obviously doing something wrong. However, there is no experience with SQL server.

Hello Valeriy.

Layers added from various SQL databases come in as OGR layers, not DBF-based Shapefiles. So when you fetch the layer from the map, you should first get it as an OGR layer, then use GetBuffer to get the layer as a Shapefile. From there, you can do your usual edits, even if you’re not saving back to the SQL source. Here is a simple sample (in VB.NET pseudo-code):

' get the layer from the map as an OGR layer
Dim ogrLayer as OgrLayer = AxMap.OgrLayer(layerIndex)
' now get the Shapefile version of that layer
dim sf as Shapefile = ogrLayer.GetBuffer()
' now that you have a Shapefile reference, you can do your usual edits
If sf.StartEditingShapes() Then
    . . .
End If

Does that help?

Hello Jerry.
Yes thanks, this code works. What you need. However, if you use

sf.StopEditingTable(True, cBack);

then an event occurs access violation in VCRUNTIME140.dll.

And this code
works perfectly.

I have another question then. And how can these changes be applied to the SQL server?

And I also checked my code that I gave at the beginning. It also code works too:

var GidrantsLayer: IShapefile = EzMap.Shapefile[GidrantsHandle]; sf.StartEditingShapes().

It turns out it’s all about this code:

sf.StopEditingTable(True, cBack); 

this code calls access violation.

Hello Valeriy.

I will have to set up a quick test to see if I can reproduce the problem. But I’m pretty sure that it is because it is a memory-based Shapefile. If you call Start/StopEditingTable directly, it attempts to update a disk-based shapefile.

Instead you should use the higher-level calls:

sf.StartEditingShapes(True, cBack);
sf.StopEditingShapes(True, True, cBack);

You can use these calls even if you’re not editing the Shapes specifically, and they will handle the differences between memory-based and disk-based saves.

It may well be that there’s a bug if you use them directly. I will look into that.

Thank you,

Hello again.

Sorry I never got to your second question. I can provide more details this weekend, but if you haven’t read it yet, you can start with the Detailed Description of OGR layer editing in the helpfile.


Thank you very much Jerry you’ve been very helpful!