Add DBF file to ArcMAP Table of Contents

9888
5
Jump to solution
06-18-2012 10:25 AM
DanWolford
New Contributor II
This is my second program using ArcObjects and I am having some technical difficulties.  I have another program I wrote that will allow you to select
tabular information from Excel and Save that to a Dbase *.dbf file where you can provide field attribute information.  This program will be an ArcMap
Add-in that will run the previously mentioned program creating a DBF file.  This DBF file will then be added to open ArcMAP file and then be joined to
an existing SHP file.  I am running 64-bit Windows 7 Professional with Visual Studio Express 2008.

The full source code is in the attached zip file and contains the four routines discussed below.  I only included the AddDBFLayer in this posting.
The routines in the code below is mostly borrowed from ESRI Samples and are all basically just to add the DBF file to ArcMAP.  Each of these programs
have there own errors that are listed in the code.  I would not be surprised if I am not properly assigning the Map Application or the Map Document
variables.  Currently I have 4 main programs called when(OnClick) button is selected.  These 4 programs are just 4 different ways I have attempted to
solve my problem, and I am looking for one good one.

1.) AddDBF - This program gives no errors but does not add the DBF to the Table of Contents?
2.) AddDBFLayer  - This program Calls AddTable which calls OpenTable.  The error occurs when trying to open the table.
3.) AddShapefile -  Error occurs when trying to open the shapefile
4.) AddShapefile2 - Error occurs when trying to open the shapefile.                  

Any and all help would be greatly appreciated.

Thanks
Dan
Imports ESRI.ArcGIS.CatalogUI Imports ESRI.ArcGIS.Framework Imports ESRI.ArcGIS.esriSystem Imports ESRI.ArcGIS.Desktop Imports ESRI.ArcGIS.ArcMapUI Imports ESRI.ArcGIS.DataSourcesFile Imports ESRI.ArcGIS.Geodatabase Imports ESRI.ArcGIS.Carto Imports ESRI.ArcGIS.Geometry Imports ESRI.ArcGIS.Display Imports System.IO Imports ESRI.ArcGIS.ADF Imports System.Drawing  Public Class btnXLStoDBF     Inherits ESRI.ArcGIS.Desktop.AddIns.Button     Private m_application As ESRI.ArcGIS.Framework.IApplication     Private mx_application As ESRI.ArcGIS.ArcMapUI.IMxApplication '*********************************************************************************************************************************************         Protected Overrides Sub OnClick()         '**EXECUTES THIS CODE WHEN BUTTON IS PRESSED IN ARCMAP         Call AddDBF()         Call AddDBFLayer()         Call AddShapefile()         Call AddShapefile2()     End Sub     Protected Overrides Sub OnUpdate()         Enabled = My.ArcMap.Application IsNot Nothing     End Sub     Public Sub AddDBFLayer()         Try             'Dim eApp As IMxApplication = My.ThisApplication             'Dim App As ESRI.ArcGIS.Framework.IApplication = My.ThisApplication             Dim pDoc As IMxDocument = My.ArcMap.Document             Dim pMap As IMap = pDoc.FocusMap             Call AddTable(pMap, pDoc)     End Sub     Public Sub AddTable(ByVal map As IMap, ByVal mxDocument As IMxDocument)         'SOURCE OF THE FOLLOWING SUBROUTINES: ArcObjects SDK 10 MS .NET Framework: Concepts and Samples         'ArcObjects Help for .NET developers\Developting with ArcGIS\Learning ArcObjects\Interacting with and configuring maps, layers, and graphics\         'Working with the Map\Performing basic map functions         'http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Performing_basic_map_functions/000100000147000000/         Dim tableCollection As ITableCollection = TryCast(map, ITableCollection)         Dim tablePathName As String = "C:\Users\dwolford\Documents\Visual Studio 2008\Projects\XLStoDBF\XLStoDBF\SHP\FINAL"         Dim tableName As String = "RT010-61-880-FINAL.DBF"         Dim table As ITable = OpenTable(tablePathName, tableName)         If (table Is Nothing) Then             Return         Else             tableCollection.AddTable(table)             mxDocument.UpdateContents()         End If     End Sub     Public Function OpenTable(ByVal pathName As String, ByVal tableName As String) As ITable         ' Create the workspace name object.             Dim workspaceName As IWorkspaceName = New WorkspaceNameClass()             workspaceName.PathName = pathName             workspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.shapefileworkspacefactory"         ' Create the table name object.             Dim dataSetName As IDatasetName = New TableNameClass()             dataSetName.Name = tableName             dataSetName.WorkspaceName = workspaceName         ' Open the table.             Dim Name As IName = TryCast(dataSetName, IName)             Dim table As ITable = TryCast(Name.Open(), ITable)  '<--ERROR HERE                     '    Error -2147220655: Exception from HRESULT: 0x80040351                     '    Systems.Collections.ListDictionaryInternal         Return table     End Function End Class
0 Kudos
1 Solution

Accepted Solutions
DanWolford
New Contributor II
Thanks argray1, I figured out what I was doing wrong with the routine.  I had the filename spelled different in one of the directories that I was testing.

View solution in original post

0 Kudos
5 Replies
AlexanderGray
Occasional Contributor III
This might be a data problem rather than a code problem.  Have you tried adding the dbf from the ArcMap add data command (not in code)?
0 Kudos
DanWolford
New Contributor II
Yes and it opened fine.  The SHP\FINAL directory contains not only a DBF but all the files for the complete shape file format.  When I am in ArcMAP and click the ADD button it will not display the dbf if it is part of a shape file (it will display the shp file extension).  So I copied just the DBF to anoth directory and added it fine.  I can add the DBF's that I created with my program, but I can't add them programmatically for some reason.
0 Kudos
AlexanderGray
Occasional Contributor III
Does the program work if the dbf is alone in the folder?
0 Kudos
DanWolford
New Contributor II
No.  I've tried changing the variables tablePathName and tableName, but it won't work.  Both the AddTable and OpenTable routines were obtained from the following link.
Arc Objects/Working with the Map/Performing Basic map functions/
So you can see what I have changed and what I haven't.
0 Kudos
DanWolford
New Contributor II
Thanks argray1, I figured out what I was doing wrong with the routine.  I had the filename spelled different in one of the directories that I was testing.
0 Kudos