Select to view content in your preferred language

Inserting Multiple Rows into Table

2490
17
Jump to solution
01-28-2013 11:06 AM
JoshObrecht
Regular Contributor
I am trying to insert multiple rows into a Geodatabase table through VB, but for some reason the program crashes when trying to enter a new row when the table already contains a row. I'm not sure if there is a lock that is occurring or what is going on. I have tried using both cursors and IFeature.store. Has this occurred for anybody else and how were you able to solve it?
0 Kudos
1 Solution

Accepted Solutions
JeffMatson
Frequent Contributor
What happens when you try it using .Insert instead of .InsertRow?

The error is occurring at tableInsertCursor = outputSignDetailTable.InsertRow(True).

View solution in original post

0 Kudos
17 Replies
KenBuja
MVP Esteemed Contributor
Please post your code. And what version of ArcGIS are you using?
0 Kudos
JoshObrecht
Regular Contributor
I am using 10.1 sp1.

Protected Overrides Sub OnClick() Implements ESRI.ArcGIS.SystemUI.ICommand.OnClick
        Dim pMxDoc As IMxDocument = TryCast(My.ArcMap.Application.Document, IMxDocument)
        Dim enumFeatureSetup As IEnumFeatureSetup
        Dim shape As String
        If pMxDoc Is Nothing Then
            Return
        End If
        Dim pMap As IMap = pMxDoc.FocusMap
        Dim pEF As IEnumFeature = pMap.FeatureSelection
        enumFeatureSetup = pEF
        enumFeatureSetup.AllFields = True
        Dim pFeat As IFeature = pEF.Next
        If pFeat Is Nothing Then
            Exit Sub
        End If
        Dim fields As IFields
        fields = pFeat.Fields
        Dim fieldCount As Integer = 0
        Dim field, ffield As IField
        Dim IDvalue, Lvalue, Mvalue, Rvalue As String
        Dim Ovalue As Integer
        Dim StrArray() As String = {"speedsigns", "intersections", "unpaved", "rumble", "paved", "medians", "lanes", "lighting", "location"}
        Do While pFeat IsNot Nothing
            If Array.IndexOf(StrArray, pFeat.Class.AliasName) = -1 Then
                Do Until fieldCount = fields.FieldCount
                    field = pFeat.Fields.Field(fieldCount)
                    If field.Name = "RandomID" Then
                        IDvalue = pFeat.Value(fieldCount)
                    ElseIf field.Name = "Lighting" Then
                        Lvalue = pFeat.Value(fieldCount)
                    ElseIf field.Name = "RumbleStri" Then
                        Rvalue = pFeat.Value(fieldCount)
                    ElseIf field.Name = "Median" Then
                        Mvalue = pFeat.Value(fieldCount)
                    ElseIf field.Name = "OBJECTID" Then
                        Ovalue = pFeat.Value(fieldCount)
                    End If
                    fieldCount += 1
                Loop
                Exit Do
            End If
            pFeat = pEF.Next
        Loop
        pEF = pMap.FeatureSelection
        enumFeatureSetup = pEF
        enumFeatureSetup.AllFields = True
        pFeat = pEF.Next
        Dim commandItem As ICommandItem
        Dim commandBars As ICommandBars
        commandBars = My.ArcMap.Application.Document.CommandBars
        commandItem = commandBars.Find(My.ThisAddIn.IDs.ArcGISAddin1, True, False)
        Dim workspacePath As String = ArcMapAddin1.ArcGISAddin3.val
        Dim workspaceFactory As IWorkspaceFactory
        Dim workspace As IWorkspace
        Try
            workspaceFactory = DirectCast(Activator.CreateInstance(Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory")), IWorkspaceFactory)
            workspace = workspaceFactory.OpenFromFile(workspacePath, My.ArcMap.Application.hWnd)
        Catch ex As Exception
            MsgBox("Database Not Found")
            Exit Sub
        End Try
        Dim rset As String = "hold"
        Dim fieldcount2 As Integer
        pFeat = pEF.Next
        shape = ArcMapAddin1.ArcGISAddin1.val
        Dim outputSignDetailTable As ITable
        Dim tableBuffer As IRowBuffer
        Dim row As IRow
        Dim feature As IRowBuffer
        Dim pEF2 As IEnumFeature = pMap.FeatureSelection
        Dim enumFeatureSetup2 As IEnumFeatureSetup
        enumFeatureSetup2 = pEF2
        enumFeatureSetup2.AllFields = True
        Dim pFeat2 As IFeature = pEF2.Next
        Dim speedsigns As Integer = 0
        Dim intersections As Integer = 0
        Dim unpaved As Integer = 0
        Dim rumble As Integer = 0
        Dim paved As Integer = 0
        Dim medians As Integer = 0
        Dim lanes As Integer = 0
        Dim lighting As Integer = 0
        Dim location As Integer = 0
        Do While pFeat2 IsNot Nothing
            If pFeat2.Class.AliasName = "speedsigns" Then
                speedsigns += 1
            ElseIf pFeat2.Class.AliasName = "intersections" Then
                intersections += 1
            ElseIf pFeat2.Class.AliasName = "unpaved" Then
                unpaved += 1
            ElseIf pFeat2.Class.AliasName = "rumble" Then
                rumble += 1
            ElseIf pFeat2.Class.AliasName = "paved" Then
                paved += 1
            ElseIf pFeat2.Class.AliasName = "medians" Then
                medians += 1
            ElseIf pFeat2.Class.AliasName = "lanes" Then
                lanes += 1
            ElseIf pFeat2.Class.AliasName = "lighting" Then
                lighting += 1
            ElseIf pFeat2.Class.AliasName = "location" Then
                location += 1
            End If
            pFeat2 = pEF2.Next()
        Loop
        Dim iii As Integer
        pEF = pMap.FeatureSelection
        enumFeatureSetup = pEF
        enumFeatureSetup.AllFields = True
        pFeat = pEF.Next
        pFeat = pEF.Next
        Dim tableInsertCursor As ICursor
        Dim newOID As Object
        Do While pFeat IsNot Nothing
            If shape = pFeat.Class.AliasName Then
                outputSignDetailTable = DirectCast(workspace, IFeatureWorkspace).OpenTable(shape)
                tableBuffer = outputSignDetailTable.CreateRowBuffer()
                row = CType(tableBuffer, IRow)
                feature = tableBuffer
                tableInsertCursor = outputSignDetailTable.InsertRow(True)
                feature.Value(0) = IDvalue
                feature.Value(1) = Ovalue
                fieldcount2 = 1
                Do Until fieldcount2 = pFeat.Fields.FieldCount
                    iii = 0
                    Do While iii < outputSignDetailTable.Fields.FieldCount
                        ffield = outputSignDetailTable.Fields.Field(iii)
                        If ffield.AliasName = pFeat.Fields.Field(fieldcount2).AliasName Then
                            feature.Value(iii) = pFeat.Value(fieldcount2)
                            Exit Do
                        End If
                        iii += 1
                    Loop
                    fieldcount2 += 1
                Loop
                If shape = "rumble" Then
                    fieldcount2 += 1
                    If rset = "hold" Then
                        If MsgBox("Are these rumble strips a set?", vbYesNo) = vbYes Then
                            rset = "yes"
                            iii = 0
                            Do While iii < outputSignDetailTable.Fields.FieldCount
                                ffield = outputSignDetailTable.Fields.Field(iii)
                                If ffield.AliasName = "Set" Then
                                    feature.Value(iii) = "SET"
                                    Exit Do
                                End If
                                iii += 1
                            Loop
                        Else
                            rset = "no"
                        End If
                    ElseIf rset = "yes" Then
                        iii = 0
                        Do While iii < outputSignDetailTable.Fields.FieldCount
                            ffield = outputSignDetailTable.Fields.Field(iii)
                            If ffield.AliasName = "Set" Then
                                feature.Value(iii) = "SET"
                                Exit Do
                            End If
                            iii += 1
                        Loop
                    End If
                End If
                If shape <> "location" Then
                    commandItem = commandBars.Find(My.ThisAddIn.IDs.Process2, True, False)
                    If commandItem.Caption = "Ambiguity On" Then
                        iii = 0
                        Do While iii < outputSignDetailTable.Fields.FieldCount
                            ffield = outputSignDetailTable.Fields.Field(iii)
                            If ffield.AliasName = "Ambiguity" Then
                                feature.Value(iii) = "AMBIG"
                                Exit Do
                            End If
                            iii += 1
                        Loop
                    End If
                End If
                commandItem = commandBars.Find(My.ThisAddIn.IDs.Process3, True, False)
                If commandItem.Caption = "Double Check On" Then
                    iii = 0
                    Do While iii < outputSignDetailTable.Fields.FieldCount
                        ffield = outputSignDetailTable.Fields.Field(iii)
                        If ffield.AliasName = "Check" Then
                            feature.Value(iii) = "CHECK"
                            Exit Do
                        End If
                        iii += 1
                    Loop
                    tableInsertCursor.InsertRow(feature)
                Else
                    tableInsertCursor.InsertRow(feature)
                End If
            End If
            pFeat = pEF.Next()
        Loop
    End Sub
0 Kudos
HalilSiddique
Frequent Contributor
I am using 10.1 sp1.

Protected Overrides Sub OnClick() Implements ESRI.ArcGIS.SystemUI.ICommand.OnClick
        Dim pMxDoc As IMxDocument = TryCast(My.ArcMap.Application.Document, IMxDocument)
        Dim enumFeatureSetup As IEnumFeatureSetup
        Dim shape As String
        If pMxDoc Is Nothing Then
            Return
        End If
        Dim pMap As IMap = pMxDoc.FocusMap
        Dim pEF As IEnumFeature = pMap.FeatureSelection
        enumFeatureSetup = pEF
        enumFeatureSetup.AllFields = True
        Dim pFeat As IFeature = pEF.Next
        If pFeat Is Nothing Then
            Exit Sub
        End If
        Dim fields As IFields
        fields = pFeat.Fields
        Dim fieldCount As Integer = 0
        Dim field, ffield As IField
        Dim IDvalue, Lvalue, Mvalue, Rvalue As String
        Dim Ovalue As Integer
        Dim StrArray() As String = {"speedsigns", "intersections", "unpaved", "rumble", "paved", "medians", "lanes", "lighting", "location"}
        Do While pFeat IsNot Nothing
            If Array.IndexOf(StrArray, pFeat.Class.AliasName) = -1 Then
                Do Until fieldCount = fields.FieldCount
                    field = pFeat.Fields.Field(fieldCount)
                    If field.Name = "RandomID" Then
                        IDvalue = pFeat.Value(fieldCount)
                    ElseIf field.Name = "Lighting" Then
                        Lvalue = pFeat.Value(fieldCount)
                    ElseIf field.Name = "RumbleStri" Then
                        Rvalue = pFeat.Value(fieldCount)
                    ElseIf field.Name = "Median" Then
                        Mvalue = pFeat.Value(fieldCount)
                    ElseIf field.Name = "OBJECTID" Then
                        Ovalue = pFeat.Value(fieldCount)
                    End If
                    fieldCount += 1
                Loop
                Exit Do
            End If
            pFeat = pEF.Next
        Loop
        pEF = pMap.FeatureSelection
        enumFeatureSetup = pEF
        enumFeatureSetup.AllFields = True
        pFeat = pEF.Next
        Dim commandItem As ICommandItem
        Dim commandBars As ICommandBars
        commandBars = My.ArcMap.Application.Document.CommandBars
        commandItem = commandBars.Find(My.ThisAddIn.IDs.ArcGISAddin1, True, False)
        Dim workspacePath As String = ArcMapAddin1.ArcGISAddin3.val
        Dim workspaceFactory As IWorkspaceFactory
        Dim workspace As IWorkspace
        Try
            workspaceFactory = DirectCast(Activator.CreateInstance(Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory")), IWorkspaceFactory)
            workspace = workspaceFactory.OpenFromFile(workspacePath, My.ArcMap.Application.hWnd)
        Catch ex As Exception
            MsgBox("Database Not Found")
            Exit Sub
        End Try
        Dim rset As String = "hold"
        Dim fieldcount2 As Integer
        pFeat = pEF.Next
        shape = ArcMapAddin1.ArcGISAddin1.val
        Dim outputSignDetailTable As ITable
        Dim tableBuffer As IRowBuffer
        Dim row As IRow
        Dim feature As IRowBuffer
        Dim pEF2 As IEnumFeature = pMap.FeatureSelection
        Dim enumFeatureSetup2 As IEnumFeatureSetup
        enumFeatureSetup2 = pEF2
        enumFeatureSetup2.AllFields = True
        Dim pFeat2 As IFeature = pEF2.Next
        Dim speedsigns As Integer = 0
        Dim intersections As Integer = 0
        Dim unpaved As Integer = 0
        Dim rumble As Integer = 0
        Dim paved As Integer = 0
        Dim medians As Integer = 0
        Dim lanes As Integer = 0
        Dim lighting As Integer = 0
        Dim location As Integer = 0
        Do While pFeat2 IsNot Nothing
            If pFeat2.Class.AliasName = "speedsigns" Then
                speedsigns += 1
            ElseIf pFeat2.Class.AliasName = "intersections" Then
                intersections += 1
            ElseIf pFeat2.Class.AliasName = "unpaved" Then
                unpaved += 1
            ElseIf pFeat2.Class.AliasName = "rumble" Then
                rumble += 1
            ElseIf pFeat2.Class.AliasName = "paved" Then
                paved += 1
            ElseIf pFeat2.Class.AliasName = "medians" Then
                medians += 1
            ElseIf pFeat2.Class.AliasName = "lanes" Then
                lanes += 1
            ElseIf pFeat2.Class.AliasName = "lighting" Then
                lighting += 1
            ElseIf pFeat2.Class.AliasName = "location" Then
                location += 1
            End If
            pFeat2 = pEF2.Next()
        Loop
        Dim iii As Integer
        pEF = pMap.FeatureSelection
        enumFeatureSetup = pEF
        enumFeatureSetup.AllFields = True
        pFeat = pEF.Next
        pFeat = pEF.Next
        Dim tableInsertCursor As ICursor
        Dim newOID As Object
        Do While pFeat IsNot Nothing
            If shape = pFeat.Class.AliasName Then
                outputSignDetailTable = DirectCast(workspace, IFeatureWorkspace).OpenTable(shape)
                tableBuffer = outputSignDetailTable.CreateRowBuffer()
                row = CType(tableBuffer, IRow)
                feature = tableBuffer
                tableInsertCursor = outputSignDetailTable.InsertRow(True)
                feature.Value(0) = IDvalue
                feature.Value(1) = Ovalue
                fieldcount2 = 1
                Do Until fieldcount2 = pFeat.Fields.FieldCount
                    iii = 0
                    Do While iii < outputSignDetailTable.Fields.FieldCount
                        ffield = outputSignDetailTable.Fields.Field(iii)
                        If ffield.AliasName = pFeat.Fields.Field(fieldcount2).AliasName Then
                            feature.Value(iii) = pFeat.Value(fieldcount2)
                            Exit Do
                        End If
                        iii += 1
                    Loop
                    fieldcount2 += 1
                Loop
                If shape = "rumble" Then
                    fieldcount2 += 1
                    If rset = "hold" Then
                        If MsgBox("Are these rumble strips a set?", vbYesNo) = vbYes Then
                            rset = "yes"
                            iii = 0
                            Do While iii < outputSignDetailTable.Fields.FieldCount
                                ffield = outputSignDetailTable.Fields.Field(iii)
                                If ffield.AliasName = "Set" Then
                                    feature.Value(iii) = "SET"
                                    Exit Do
                                End If
                                iii += 1
                            Loop
                        Else
                            rset = "no"
                        End If
                    ElseIf rset = "yes" Then
                        iii = 0
                        Do While iii < outputSignDetailTable.Fields.FieldCount
                            ffield = outputSignDetailTable.Fields.Field(iii)
                            If ffield.AliasName = "Set" Then
                                feature.Value(iii) = "SET"
                                Exit Do
                            End If
                            iii += 1
                        Loop
                    End If
                End If
                If shape <> "location" Then
                    commandItem = commandBars.Find(My.ThisAddIn.IDs.Process2, True, False)
                    If commandItem.Caption = "Ambiguity On" Then
                        iii = 0
                        Do While iii < outputSignDetailTable.Fields.FieldCount
                            ffield = outputSignDetailTable.Fields.Field(iii)
                            If ffield.AliasName = "Ambiguity" Then
                                feature.Value(iii) = "AMBIG"
                                Exit Do
                            End If
                            iii += 1
                        Loop
                    End If
                End If
                commandItem = commandBars.Find(My.ThisAddIn.IDs.Process3, True, False)
                If commandItem.Caption = "Double Check On" Then
                    iii = 0
                    Do While iii < outputSignDetailTable.Fields.FieldCount
                        ffield = outputSignDetailTable.Fields.Field(iii)
                        If ffield.AliasName = "Check" Then
                            feature.Value(iii) = "CHECK"
                            Exit Do
                        End If
                        iii += 1
                    Loop
                    tableInsertCursor.InsertRow(feature)
                Else
                    tableInsertCursor.InsertRow(feature)
                End If
            End If
            pFeat = pEF.Next()
        Loop
    End Sub


Josh,
Are you sure you can add your own OBJECTID values, I thought it was generated automatically?
0 Kudos
JoshObrecht
Regular Contributor
I'm not adding my own OBJECTID. I'm taking one feature's OBJECTID and another feature's attributes and writing both to a table row.
0 Kudos
KenBuja
MVP Esteemed Contributor
The first thing to do is the put this into a Try Catch block so you know why and where the program is crashing.

For the second and third time you have the line

pEF = pMap.FeatureSelection (and why not use pEF.Reset?)

why do you call pEF.Next twice? Are you testing whether you have at least two features selected?
0 Kudos
JoshObrecht
Regular Contributor
The pEF.Next twice is a remnant and should not be in there anymore and I did not see that there was a pEF.Reset. Thanks. The error is occurring at tableInsertCursor.InsertRow(feature) with the message "Object reference not set to an instance of an Object."
0 Kudos
JoshObrecht
Regular Contributor
As I said, if there is already a row in the table, it will not allow it errors on the InsertRow.
0 Kudos
NeilClemmons
Honored Contributor
feature.Value(0) = IDvalue

Where are you declaring "feature"?  As far as I can see it's not being declared (which would explain the object not set error) and you don't need it.  If it is supposed to be an IFeature object then that would be incorrect as you can't add a feature to a table.  Also, the field at index 0 is usually the ObjectId field. Is this true for your table?  If so, the ObjectId field is readonly.  If you try to set its value then you will get an error.  ObjectIds are managed by the database.
0 Kudos
JeffMatson
Frequent Contributor
First thing I would do is move this line up so it is outside of your loop:


outputSignDetailTable = DirectCast(workspace, IFeatureWorkspace).OpenTable(shape)
0 Kudos