update atribut

544
6
04-19-2013 12:04 AM
HaniuHokkaido
New Contributor III
Hi,

I found a code like this:

==start code=============
  Public Function InsertPoint(ByVal name As String, ByVal x As Double, ByVal y As Double, ByVal fld As String, ByVal fldval As String) As String
        Dim sc As IServerContext = Nothing
        Dim r As String
        Try
            Dim idt As ESRI.ArcGIS.ADF.Identity = New ESRI.ArcGIS.ADF.Identity("username", "password", "domain")
            Dim agc As ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection = New ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection("server", idt)
            agc.Connect()
            Dim sm As IServerObjectManager = agc.ServerObjectManager
            sc = sm.CreateServerContext("basemap", "MapServer")
            Dim so As IServerObject = sc.ServerObject
            Dim prop As IPropertySet = sc.CreateObject("esriSystem.PropertySet")

            'connect to db
            prop.SetProperty("SERVER", "svr-955")
            prop.SetProperty("INSTANCE", "esri_sde")
            prop.SetProperty("DATABASE", "reg_opt")
            prop.SetProperty("USER", "sde")
            prop.SetProperty("PASSWORD", "sde")
            prop.SetProperty("VERSION", "sde.DEFAULT")
            'prop.SetProperty("VERSION", "SDE.test")

            Dim wsf As IWorkspaceFactory = sc.CreateObject("esriDataSourcesGDB.SdeWorkspaceFactory")
            Dim fws As IFeatureWorkspace = wsf.Open(prop, 0)
            Dim fc As IFeatureClass = fws.OpenFeatureClass(name)

            If Not fc Is Nothing Then
                If fc.ShapeType = esriGeometryType.esriGeometryMultipoint Or fc.ShapeType = esriGeometryType.esriGeometryPoint Then
                    Dim i As Integer = fc.FindField(fld)

                    'insert new object
                    If i > -1 Then
                        Dim frc As IFeatureCursor = fc.Insert(False)
                        Dim ft As IFeature = fc.CreateFeature()
                        Dim pnt As IPoint = sc.CreateObject("esriGeometry.Point")
                        pnt.X = x
                        pnt.Y = y
                        ft.Value(i) = fldval
                        ft.Shape = pnt
                        ft.Store()
                        r = "success"
                    Else
                        r = "error: field not found."
                    End If

                Else
                    r = "error: data not point."
                End If

            Else
                r = "error: data not found."
            End If
            sc.ReleaseContext()
        Catch ex As Exception
            r = "error:" & ex.Message
        End Try
        Return r
    End Function

==end code=============

I want to replace the "insert new object" with "update existing object"

How can i achieve that ?

many many thanks
0 Kudos
6 Replies
KenBuja
MVP Esteemed Contributor
You can use the Insert method on the featureclass. Some examples of how to use this can be found here and herehttp://resources.esri.com/help/9.3/arcgisengine/arcobjects/esriGeoDatabase/IFeatureClass.Update_Exam....
0 Kudos
HaniuHokkaido
New Contributor III
Hi Ken,

Actually, i am trying to edit an attribute without looking at the spatial, just like editing a non-spatial table. The flow is like this: find a value, if found then update it with a new value

Do you have a sample ?

thanks
0 Kudos
sameerpuppal
Occasional Contributor
Hi Ken,

Actually, i am trying to edit an attribute without looking at the spatial, just like editing a non-spatial table. The flow is like this: find a value, if found then update it with a new value

Do you have a sample ?

thanks


 protected override void Update(IFeatureClass pFClass,string Query,string Value)
        {
            IQueryFilter pQF = new QueryFilterClass();
            pQF.WhereClause = Query;//"your query goes here";// eg = name = 'something'
            IFeatureCursor pFeatureCursor = pFClass.Search(pQF,false)
            IFeature pFeature = null;
            int indexFieldinWhichtoUpd = pFClass.Fields.FindField("FieldToUpd");
            while ((pFeature = pFeatureCursor.NextFeature())!=null)
            {
                pFeature.set_Value(indexFieldinWhichtoUpd,Value);
                pFeature.Store();
            }
        }


programatically start edit at start and stop edit after this method. this sample will update value of the field - fieldtoupd with value - value for all feature satisfying query. in featureclass pfclass.


Regards,
Sameer Puppal

Mark answer if this is what you wanted!
0 Kudos
HaniuHokkaido
New Contributor III
Hi,

here i use your suggestion into my code:

Dim qf As IQueryFilter = sc.CreateObject("esriGeoDatabase.QueryFilter")
qf.WhereClause = "field1 = " & "'" & thevalue & "'"

'http://forums.arcgis.com/threads/7393-IQueryFilter.Subfields-does-not-work.
qf.SubFields = colomnname 'qf.subfields = "*" 'to show all columns
qf.AddField("field1")
qf.AddField("field2")
qf.AddField("field3")

'Debug.Print(qf.SubFields)
Dim fr As IFeatureCursor = fc.Search(qf, False)

Dim ft As IFeature
Do
ft = fr.NextFeature()
If Not (ft Is Nothing) Then

'update the value
ft.Value(1) = "thenewdatavalue"
ft.Store()

End If
Loop Until (ft Is Nothing)

It gives me error at ft.Store. The error is : Objects in this class cannot be updated outside an edit session

Apparently i have to start an edit session. How can i do that ?

thanks alot
0 Kudos
HaniuHokkaido
New Contributor III
to complete my posting above, here is the full code. I implement start editing already but it gives me error "error:Object reference not set to an instance of an object"

Public Function Updateattrib(ByVal layername As String, ByVal coloumn As String, ByVal myval As String) As String

Dim sc As IServerContext = Nothing
Dim rs As String = ""
Dim r As String

Try
Dim idt As ESRI.ArcGIS.ADF.Identity = New ESRI.ArcGIS.ADF.Identity("myusername", "pwd", "svr-955")
Dim agc As ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection
agc = New ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection("svr-955", idt)
agc.Connect()
If (Not agc.IsConnected) Then
MsgBox("not connected")
End If

Dim sm As IServerObjectManager = agc.ServerObjectManager
sc = sm.CreateServerContext("amapservicename", "MapServer")
Dim so As IServerObject = sc.ServerObject
Dim prop As IPropertySet = sc.CreateObject("esriSystem.PropertySet")

'db conn
prop.SetProperty("SERVER", "svr-955")
prop.SetProperty("INSTANCE", "esri_sde")
prop.SetProperty("DATABASE", "reg_opt")
prop.SetProperty("USER", "sde")
prop.SetProperty("PASSWORD", "sde")
prop.SetProperty("VERSION", "sde.DEFAULT")
'prop.SetProperty("VERSION", "SDE.test")

Dim wsf As IWorkspaceFactory = sc.CreateObject("esriDataSourcesGDB.SdeWorkspaceFactory")
Dim fws As IFeatureWorkspace = wsf.Open(prop, 0)
Dim fc As IFeatureClass = fws.OpenFeatureClass(namalayer)

Dim ft As IFeature
Dim tbl As ITable = Nothing
Dim workspc As IWorkspace = Nothing

' Cast the workspace to the IMultiuserWorkspaceEdit and IWorkspaceEdit2 interfaces.
Dim muWorkspaceEdit As IMultiuserWorkspaceEdit = CType(workspc, IMultiuserWorkspaceEdit)
Dim workspaceEdit As IWorkspaceEdit = CType(workspc, IWorkspaceEdit2)

' Start a versioned edit session.
muWorkspaceEdit.StartMultiuserEditing(esriMultiuserEditSessionMode.esriMESMVersioned) 'error here

' Start an edit operation.
workspaceEdit.StartEditOperation()

If Not fc Is Nothing Then
Dim i As Integer = fc.FindField(kolom)
If i > -1 Then
Dim qf As IQueryFilter = sc.CreateObject("esriGeoDatabase.QueryFilter")
qf.WhereClause = "field1 = " & "'" & myval & "'" 'field1 is the name of the field in amapservicename

qf.SubFields = coloumnname 'qf.subfields = "*"
qf.AddField("field1")
qf.AddField("field2")
qf.AddField("field3")


Dim fr As IFeatureCursor = fc.Search(qf, False)
'Dim fr As IFeatureCursor = fc.Update(qf,False)

Dim indexfieldtarget As Integer
indexfieldtarget = fc.Fields.FindField("field1") 'target field index
'Debug.Print("targetindex :" & indexfieldtarget)

'print the result
Do
ft = fr.NextFeature()
If Not (ft Is Nothing) Then

'update the value
ft.Value(indexfieldtarget) = "mydata"
'fr.UpdateFeature(ft)
ft.Store()

End If
Loop Until (ft Is Nothing)
If Len(rs) > 1 Then
rs = Right(rs, Len(rs) - 1)
End If

Else
rs = "error:field not found"
End If
Else
rs = "error:data not found"
End If

' Save the edit operation. To cancel an edit operation, the AbortEditOperation
' method can be used.
workspaceEdit.StopEditOperation()
workspaceEdit.StopEditing(True)

sc.ReleaseContext()

Catch ex As Exception
rs = "error:" & ex.Message
End Try
Return rs
End Function
0 Kudos
HaniuHokkaido
New Contributor III
Anyone with idea / suggestion ?
0 Kudos