Problem connecting with FileGeoDB table

3219
2
Jump to solution
04-26-2012 12:13 PM
SteveCole
Frequent Contributor
I have a small subroutine in a VB.NET add-in I'm developing and it's sole purpose is to open a standalone table stored in a file geodatabase and add a new record to it. I searched through help and the forum to cobble together some similar code which compiles but errors once it's called. Here's the subroutine:

Public Sub addPhotoToGeoDatabase(ByVal theGTagInfo As geoTaggedInfo)         Try             Dim ptable As ITable              ' Create a file geodatabase workspace factory.             Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")             Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)              ' Create a file geodatabase.             Dim workspaceName As IWorkspaceName = workspaceFactory.OpenFromFile("G:\PublicWk\geoTagging\pwPhotoDatabaseTest.gdb", 0)              ' Open the geodatabase using the name object.             Dim Name As ESRI.ArcGIS.esriSystem.IName = CType(workspaceName, ESRI.ArcGIS.esriSystem.IName)             Dim workspace As IWorkspace = CType(Name.Open(), IWorkspace)              ' Cast the workspace to the IWorkspaceEdit2 interface.             Dim workspaceEdit As IWorkspaceEdit = CType(workspace, IWorkspaceEdit)              ' Start an edit session. An undo/redo stack isn't necessary in this case.             workspaceEdit.StartEditing(False)              ' Start an edit operation.             workspaceEdit.StartEditOperation()             ptable = workspace.OpenTable("tblGeotaggedPhotos")              ' Create a row. The row's attribute values should be set here and if             ' a feature is being created, the shape should be set as well.             Dim row As IRow = ptable.CreateRow()             row.Value("Latitude") = theGTagInfo.latitude             row.Value("Longitude") = theGTagInfo.longitude             row.Value("Descrip") = theGTagInfo.description             row.Value("Keywords") = theGTagInfo.keywords             row.Value("gTagByUser") = theGTagInfo.taggedByUserName             row.Value("gTagByFullName") = theGTagInfo.tageedByFullName             row.Value("DateGeotagged") = theGTagInfo.dateTagged             row.Value("FileName") = theGTagInfo.filename             row.Value("FullPath") = theGTagInfo.fullpath             row.Store()              ' Save the edit operation. To cancel an edit operation, the AbortEditOperation             ' method can be used.             workspaceEdit.StopEditOperation()              ' Stop the edit session. The saveEdits parameter indicates the edit session             ' will be committed.             workspaceEdit.StopEditing(True)              ptable = Nothing             row = Nothing             workspace = Nothing             workspaceEdit = Nothing             factoryType = Nothing             workspaceFactory = Nothing         Catch ex As Exception             globalErrorHandler(ex)         End Try     End Sub


and I have the following imported at the top of my module:

Imports ESRI.ArcGIS.Geodatabase Imports ESRI.ArcGIS.Carto


When the subroutine is called, it gets to the following line and crashes:

Dim workspaceName As IWorkspaceName = workspaceFactory.OpenFromFile("G:\PublicWk\geoTagging\pwPhotoDatabaseTest.gdb", 0)


The exact error returned is..

ERROR DESCRIPTION:  Unable to cast COM object of type 'System.__ComObject' to interface type 'ESRI.ArcGIS.Geodatabase.IWorkspaceName'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{FADD975C-E36F-11D1-AA81-00C04FA33A15}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
TYPE OF ERROR:   System.InvalidCastException
STACK TRACE:   at geoTagPhotos2.usrFunctions.addPhotoToGeoDatabase(geoTaggedInfo theGTagInfo) in C:\Visual Studio 2008\Projects\geoTagPhotos2\geoTagPhotos2\usrFunctions.vb:line 407


So what am I doing wrong?

I was using a code snippit for creating a file geodatabase and I noticed that in that example, there's an ampersand (@) before the file path. Didn't know what that was about and if I needed it while using the OpenFromFile method.

Thanks!
Steve
0 Kudos
1 Solution

Accepted Solutions
NeilClemmons
Regular Contributor III
We still haven't made the switch to ArcGIS 10 yet as we are waiting on clients, but unless things have really changed here's what I see wrong...

' Create a file geodatabase.
Dim workspaceName As IWorkspaceName = workspaceFactory.OpenFromFile("G:\PublicWk\geoTagging\pwPhotoDatabaseTest.gdb", 0)

This is incorrect.  The OpenFromFile method returns an IWorkspace reference, not IWorkspaceName.  You should replace your current code with this:

Dim workspace As IWorkspace = workspaceFactory.OpenFromFile("G:\PublicWk\geoTagging\pwPhotoDatabaseTest.gdb", 0)

This is also incorrect:
ptable = workspace.OpenTable("tblGeotaggedPhotos")

IWorkspace does not have an OpenTable method.  You need to QI to IFeatureWorkspace for that:
ptable = DirectCast(workspace, IFeatureWorkspace).OpenTable("tblGeotaggedPhotos")

The last thing I see is:
row.Value("Latitude") = theGTagInfo.latitude

The Value method takes a field index, not a field name.

Also, there is no need to set all your variables to Nothing at the end as this will happen by default when the routine exits.  If things still don't work after you make these changes then post your updated code and I'll take another look.

View solution in original post

0 Kudos
2 Replies
NeilClemmons
Regular Contributor III
We still haven't made the switch to ArcGIS 10 yet as we are waiting on clients, but unless things have really changed here's what I see wrong...

' Create a file geodatabase.
Dim workspaceName As IWorkspaceName = workspaceFactory.OpenFromFile("G:\PublicWk\geoTagging\pwPhotoDatabaseTest.gdb", 0)

This is incorrect.  The OpenFromFile method returns an IWorkspace reference, not IWorkspaceName.  You should replace your current code with this:

Dim workspace As IWorkspace = workspaceFactory.OpenFromFile("G:\PublicWk\geoTagging\pwPhotoDatabaseTest.gdb", 0)

This is also incorrect:
ptable = workspace.OpenTable("tblGeotaggedPhotos")

IWorkspace does not have an OpenTable method.  You need to QI to IFeatureWorkspace for that:
ptable = DirectCast(workspace, IFeatureWorkspace).OpenTable("tblGeotaggedPhotos")

The last thing I see is:
row.Value("Latitude") = theGTagInfo.latitude

The Value method takes a field index, not a field name.

Also, there is no need to set all your variables to Nothing at the end as this will happen by default when the routine exits.  If things still don't work after you make these changes then post your updated code and I'll take another look.
0 Kudos
SteveCole
Frequent Contributor
Thanks, Neil. You were correct in all of your points. Sometimes the ESRI Help Docs are more confusing than helpful and that OpenFromFile for FileGeodatabases was one of those times for me. While waiting for replies in my thread, I found another thread on an unrelated topic but it helped me debug my problem. Here's my revised code which I have tested and works:

    Public Sub addPhotoToGeoDatabase(ByVal theGTagInfo As geoTaggedInfo)
        Try
            Dim ptable As ITable
            Dim dbPath As String = "G:\PublicWk\geoTagging\pwPhotoDatabaseTest.gdb"

            ' Create a file geodatabase workspace factory.
            Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")

            Dim WorkspaceFactory As IWorkspaceFactory = New ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactory
            Dim Workspace As IFeatureWorkspace = CType(WorkspaceFactory.OpenFromFile(dbPath, 0), IFeatureWorkspace)

            ' Cast the workspace to the IWorkspaceEdit2 interface.
            Dim workspaceEdit As IWorkspaceEdit = CType(workspace, IWorkspaceEdit)

            ' Start an edit session. An undo/redo stack isn't necessary in this case.
            workspaceEdit.StartEditing(False)

            ' Start an edit operation.
            workspaceEdit.StartEditOperation()
            ptable = workspace.OpenTable("tblGeotaggedPhotos")

            ' Create a row. The row's attribute values should be set here and if
            ' a feature is being created, the shape should be set as well.
            Dim row As IRow = ptable.CreateRow()
            row.Value(ptable.FindField("Latitude")) = theGTagInfo.latitude
            row.Value(ptable.FindField("Longitude")) = theGTagInfo.longitude
            row.Value(ptable.FindField("Descrip")) = theGTagInfo.description
            row.Value(ptable.FindField("Keywords")) = theGTagInfo.keywords
            row.Value(ptable.FindField("gTagByUser")) = theGTagInfo.taggedByUserName
            row.Value(ptable.FindField("gTagByFullName")) = theGTagInfo.tageedByFullName
            row.Value(ptable.FindField("gTagDate")) = theGTagInfo.dateTagged
            row.Value(ptable.FindField("FileName")) = theGTagInfo.filename
            row.Value(ptable.FindField("FullPath")) = theGTagInfo.fullpath
            row.Store()

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

            ' Stop the edit session. The saveEdits parameter indicates the edit session
            ' will be committed.
            workspaceEdit.StopEditing(True)
        Catch ex As Exception
            globalErrorHandler(ex)
        End Try
    End Sub


Thanks also for the tip about setting variables to NOTHING. I'm coming from a lengthy time in VBA (Access & Arcobjects) so that's almost second nature to me. Great to know I no longer need to do that!

Steve
0 Kudos