# Change the field value on layer from SQL

**URL:** <https://mapwindow.discourse.group/t/change-the-field-value-on-layer-from-sql/435>\
**Category:** MapWinGIS\
**Created:** [January 30, 2020, 2:40pm UTC](https://mapwindow.discourse.group/t/change-the-field-value-on-layer-from-sql/435 "2020-01-30T14:40:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mainxt](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/mainxt/32/33_2.png) [@mainxt](https://mapwindow.discourse.group/u/mainxt)\
**Post date:** [January 30, 2020, 2:40pm UTC](https://mapwindow.discourse.group/t/change-the-field-value-on-layer-from-sql/435/1 "2020-01-30T14:40:03Z")

</div>

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];
GidrantsLayer.StartEditingTable(cBack);
var EzTable: Table = GidrantsLayer.Table;
EzTable.EditCellValue(FieldIndexByName['ID_TYPE_PG'], MwShapeId, 4);
GidrantsLayer.StopEditingTable(True, cBack);
AxMap.Redraw;

```

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

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [January 30, 2020, 8:12pm UTC](https://mapwindow.discourse.group/t/change-the-field-value-on-layer-from-sql/435/2 "2020-01-30T20:12:09Z")

</div>

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](https://www.mapwindow.org/documentation/mapwingis4.9/group __map__ layer__management.html#gaa1bba7d8c6fdd51e9a5cb89d485ad3b7), then use [GetBuffer](https://www.mapwindow.org/documentation/mapwingis4.9/class_ogr_layer.html#a1a2ffbbd78168673391a0b1db054ae43) 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](http://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?

---

<div class="post-metadata">

**Author:** ![mainxt](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/mainxt/32/33_2.png) [@mainxt](https://mapwindow.discourse.group/u/mainxt)\
**Post date:** [January 31, 2020, 5:02am UTC](https://mapwindow.discourse.group/t/change-the-field-value-on-layer-from-sql/435/3 "2020-01-31T05:02:35Z")

</div>

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

```
sf.StartEditingTable(cBack);
sf.StopEditingTable(True, cBack);

```

then an event occurs access violation in VCRUNTIME140.dll.

And this code  
`sf.StartEditingShapes()`  
works perfectly.

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

P.S.  
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.StartEditingTable(cBack);
sf.StopEditingTable(True, cBack); 

```

this code calls access violation.

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [January 31, 2020, 6:54pm UTC](https://mapwindow.discourse.group/t/change-the-field-value-on-layer-from-sql/435/4 "2020-01-31T18:54:40Z")

</div>

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,  
Jerry.

---

<div class="post-metadata">

**Author:** ![jerryfaust](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/jerryfaust/32/378_2.png) [@jerryfaust](https://mapwindow.discourse.group/u/jerryfaust)\
**Post date:** [February 1, 2020, 12:13am UTC](https://mapwindow.discourse.group/t/change-the-field-value-on-layer-from-sql/435/5 "2020-02-01T00:13:06Z")

</div>

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](https://www.mapwindow.org/documentation/mapwingis4.9/group __ogrlayer__ editing.html) in the helpfile.

Regards,  
Jerry.

---

<div class="post-metadata">

**Author:** ![mainxt](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/mainxt/32/33_2.png) [@mainxt](https://mapwindow.discourse.group/u/mainxt)\
**Post date:** [February 1, 2020, 3:35am UTC](https://mapwindow.discourse.group/t/change-the-field-value-on-layer-from-sql/435/6 "2020-02-01T03:35:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pmeems](https://yyz2.discourse-cdn.com/free1/user_avatar/mapwindow.discourse.group/pmeems/32/9_2.png) [@pmeems](https://mapwindow.discourse.group/u/pmeems)\
**Post date:** [January 5, 2022, 3:08pm UTC](https://mapwindow.discourse.group/t/change-the-field-value-on-layer-from-sql/435/7 "2022-01-05T15:08:58Z")

</div>


