Jump to content

 
Photo

ArcGIS .net & C#

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Ohayo

Ohayo

    Contributor

  • Validated Member
  • PipPip
  • 14 posts
  • Thailand

Hi,

A friend who is developed a script ask:
This script was using C# on ArcGIS.net platform and web ADF.net framework for link to SDE database on ArcGIS Server A.
Then it got a problem at line with red text . Can anyone help me solve this problem?

---------------------------------------------------------------------------------------------------------------------
public bool CreateTabelPrim(IServerContext context)
{
try
{
//strTableName = LayerHeader + "ILPRIM";
pTable = FeatureWorkspace.OpenTable(TablePrim);
return true;
}
catch
{
IObjectClassDescription ocDesciption =
context.CreateObject("esriGeodatabase.ObjectClassDescription") as IObjectClassDescription;

IFields pFields = context.CreateObject("esriGeoDatabase.Fields") as IFields;
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
IField pField = context.CreateObject("esriGeoDatabase.Field") as IField;

IFieldEdit pFieldEdit = context.CreateObject("esriGeoDatabase.Field") as IFieldEdit;
pFieldEdit = pField as IFieldEdit;
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
pFieldEdit.Name_2 = "ILPRIMTYPE";
pFieldEdit.IsNullable_2 = false;
pFieldEdit.Editable_2 = true;
pFieldsEdit.AddField(pFieldEdit);

pFieldEdit = context.CreateObject("esriGeoDatabase.Field") as IFieldEdit;
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldEdit.Name_2 = "ILPRIMTYPENAME";
pFieldEdit.Length_2 = 50;
pFieldEdit.IsNullable_2 = false;
pFieldEdit.Editable_2 = true;
pFieldsEdit.AddField(pFieldEdit);

pFieldEdit = context.CreateObject("esriGeoDatabase.Field") as IFieldEdit;
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldEdit.Name_2 = "ILLASTDATE";
pFieldEdit.Length_2 = 30;
pFieldEdit.IsNullable_2 = true;
pFieldEdit.Editable_2 = true;
pFieldsEdit.AddField(pFieldEdit);

//ITable pTable = (ITable)FeatureWorkspace.CreateTable(TablePrim, pFields, pUID, null, "");
ITable pTable = (ITable)FeatureWorkspace.CreateTable(TablePrim, pFields, ocDesciption.InstanceCLSID, ocDesciption.ClassExtensionCLSID, "");

//Open an insert currsor on the new table
ICursor pCursor = pTable.Insert(true);
// Create a row buffer for the row inserts
IRowBuffer pRowBuffer = pTable.CreateRowBuffer();

int i = pRowBuffer.Fields.FindField("ILPRIMTYPE");
pRowBuffer.set_Value(pRowBuffer.Fields.FindField("ILPRIMTYPE"), 0);
pRowBuffer.set_Value(pRowBuffer.Fields.FindField("ILPRIMTYPENAME"), "Point,CG");
pRowBuffer.set_Value(pRowBuffer.Fields.FindField("ILLASTDATE"), DateTime.Now.ToString());
pCursor.InsertRow(pRowBuffer);

pRowBuffer = pTable.CreateRowBuffer();
pRowBuffer.set_Value(pRowBuffer.Fields.FindField("ILPRIMTYPE"), 1); Error Field is not editable
pRowBuffer.set_Value(pRowBuffer.Fields.FindField("ILPRIMTYPENAME"), "Line");
pRowBuffer.set_Value(pRowBuffer.Fields.FindField("ILLASTDATE"), DateTime.Now.ToString());
pCursor.InsertRow(pRowBuffer);

pRowBuffer = pTable.CreateRowBuffer();
pRowBuffer.set_Value(pRowBuffer.Fields.FindField("ILPRIMTYPE"), 2);
pRowBuffer.set_Value(pRowBuffer.Fields.FindField("ILPRIMTYPENAME"), "Polyline");
pRowBuffer.set_Value(pRowBuffer.Fields.FindField("ILLASTDATE"), DateTime.Now.ToString());
pCursor.InsertRow(pRowBuffer);


pRowBuffer = pTable.CreateRowBuffer();
pRowBuffer.set_Value(pRowBuffer.Fields.FindField("ILPRIMTYPE"), 3);
pRowBuffer.set_Value(pRowBuffer.Fields.FindField("ILPRIMTYPENAME"), "Polygon");
pRowBuffer.set_Value(pRowBuffer.Fields.FindField("ILLASTDATE"), DateTime.Now.ToString());
pCursor.InsertRow(pRowBuffer);

pCursor.Flush();
return false;
}
}

#2
ceicher

ceicher

    Contributor

  • Validated Member
  • PipPip
  • 44 posts
  • Gender:Male
  • Location:Charlottesville, Virginia
  • United States

Hi Ohayo,

You created this field with type OID.... this type of field is not editable.

pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
pFieldEdit.Name_2 = "ILPRIMTYPE";


Recommendation:

Use CreateRowBuffer to create a new row buffer object, then set all values EXCEPT the OID. Then call ICursor.InsertRow. This will give you back the OID. The OID is "auto incremented" by the underlying data source.

If you want to define your own ID, you need to use a different esriFieldType, for example esriFieldType.esriFieldTypeInteger.

I do not know why you were able to set this value and insert the first row...

int i = pRowBuffer.Fields.FindField("ILPRIMTYPE");
pRowBuffer.set_Value(pRowBuffer.Fields.FindField("ILPRIMTYPE"), 0);


... but it failed the second time. The important thing is you cannot change values of OID fields.

Good luck,

-Cory

#3
Ohayo

Ohayo

    Contributor

  • Validated Member
  • PipPip
  • 14 posts
  • Thailand

HI Cory,
Thanks for your recommendation, I 'll try to test this and will let you know the result of it.


Hi Ohayo,

You created this field with type OID.... this type of field is not editable.

pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
pFieldEdit.Name_2 = "ILPRIMTYPE";


Recommendation:

Use CreateRowBuffer to create a new row buffer object, then set all values EXCEPT the OID. Then call ICursor.InsertRow. This will give you back the OID. The OID is "auto incremented" by the underlying data source.

If you want to define your own ID, you need to use a different esriFieldType, for example esriFieldType.esriFieldTypeInteger.

I do not know why you were able to set this value and insert the first row...

int i = pRowBuffer.Fields.FindField("ILPRIMTYPE");
pRowBuffer.set_Value(pRowBuffer.Fields.FindField("ILPRIMTYPE"), 0);


... but it failed the second time. The important thing is you cannot change values of OID fields.

Good luck,

-Cory



#4
inquiry

inquiry

    Newbie

  • New Member
  • Pip
  • 1 posts
  • No Country Selected

I ported some code from vb6 to .net.

There I have a statement that uses cursor to insert row.

lOID = pCursor.InsertRow(pBuff)

It works fine in most of the cases but for one table it freezes on this statement. It doesn't throw any exception.
The problem occurs frequently but randomly i.e. sometimes the statement works fine for that table also.

thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

-->