ThinkGeo.com    |     Documentation    |     Premium Support

MsSql2008FeatureLayer Error

Hi,

I’ve created a local sql db with the following simple table schema

CREATE TABLE [dbo].[Shape]
(
[Id] INT NOT NULL PRIMARY KEY,
[FeatureId] [sys].[geometry] NULL
)

The attached project is a simple attempt to write a feature to this table and to then read it back and display it on the map. However, when I try and read back the features from the MsSql2008FeatureLayer feature source, I get the error:

“An item with the same key has already been added.”

Looking at the database, the records are going in as I would expect and there’s no duplication other than the geometry itself. Here’s a screen shot.

What’s this error about and how can I fix it?

Also, I found that since I have Id as a primary key, I must explicitly update the value as I’ve done in the code or the feature is not written to the db. Since this should be an automatically updated number, do you see any reason why I must be doing this to get it to save?

DatabaseToMap.zip (2.3 MB)

Thanks,
Damian

Hi Damian,

Thanks for your samples, I can reproduce your problem, the reason is:

  1. With the MsSql2008FeatureLayer constructor, you are setting your table ‘geometry’ column to the ‘featureId’ column, this cause the duplicate query column result, then add to return collection cause duplicate key conflict.
    in the constructor, we should set the value with your primary key, like: MsSql2008FeatureLayer db = new MsSql2008FeatureLayer(conn, “Shape”, “Id”);
    And also our default ‘geometry’ column name is ‘geom’, and if you changed that, you need set db.CustomGeometryColumnName = “FeatureId”;

  2. For the primary key, if you don’t want to update it, you just need set it as auto update when you create this table, like:
    CREATE TABLE [dbo].[Shape]
    (
    [Id] INT identity(1,1) constraint pkid NOT NULL PRIMARY KEY,
    [FeatureId] [sys].[geometry] NULL
    )
    then you can just simply add feature to edittools
    db.EditTools.Add(new Feature(new PointShape(500, 2000)));

  3. I also noticed if you click the button2(display feature), it will throw ‘An item with the same key has already been added.’ error too, but this is because you add the layer to overlay multiple times, I’m not sure if this is one problem you met.

As below is a sample https://ap.thinkgeo.com:5001/sharing/DYuAWBtN6

Thanks

Hi Don,

Thanks, that’s perfect. Got it all running smoothly now.

Best Regards,
Damian

Hi Damian,

Any question please let us know.

Regards,

Don