Hi everyone,
I am using ShapefileWorkspaceFactory to retrieve data from a dbf file. Now I can read data of some specified field (by field name), I have no idea how to get corresponding value with the maximum value of a field.
For example, in the dbf file I retrieve 3 fields of data: RegisterID, ItemName, Category
I am asking how to get the ItemName and Category with the maximum RegisterID.
I am using vb.net arcobjects. Thanks in advance.
Solved! Go to Solution.
If you know the maximum RegisterID value that could become the where clause of a QueryFilter object which you feed into the Table.Search() method to get the cursor which would return a single row.
If you do not know the maximum value of RegisterID then you could use the IDataStatistics interface and return the maximum value. You do not say which version of ArcMap you are using, I ask this as ESRI introduced a bug in this interface with a service pack release for 10.1
If you know the maximum RegisterID value that could become the where clause of a QueryFilter object which you feed into the Table.Search() method to get the cursor which would return a single row.
If you do not know the maximum value of RegisterID then you could use the IDataStatistics interface and return the maximum value. You do not say which version of ArcMap you are using, I ask this as ESRI introduced a bug in this interface with a service pack release for 10.1
Hi Duncan,
Thanks a lot for the reply. I am using 10.2.2.
Hi Duncan,
I have successfully got the max OBJECTID by using IDataStatistics. But when I tried to retrieved the related values of the max OBJECTID, I got the following error:
System.Runtime.InteropServices.COMException (0x80040207): The owner SID on a per-user subscription doesn't exist (Exception from HRESULT: 0x80040207)
at ESRI.ArcGIS.Geodatabase.ITable.Search(IQueryFilter QueryFilter, Boolean Recycling)
My code is as follows:
Dim pWSF As ShapefileWorkspaceFactory = New ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory() |
Dim pWS As ESRI.ArcGIS.Geodatabase.IWorkspace = pWSF.OpenFromFile("D:/dev", 0)
Dim pFws As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace = CType(pWS, ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)
Dim pTable As ESRI.ArcGIS.Geodatabase.ITable = pFws.OpenTable("Segment_line_intersect")
Dim pCur As ESRI.ArcGIS.Geodatabase.ICursor = pTable.Search(Nothing, False) ' open the cursor
Dim queryFilter As IQueryFilter = New QueryFilterClass()
queryFilter.SubFields = "Segment_ID,LineName"
queryFilter.WhereClause = "FID_Segmen = '" + MaxOBJECTID.ToString + "'"
pCur = pTable.Search(queryFilter, True) ' error this line
Return
I cannot figure out what's wrong with the code. Is this the bug you mentioned? Thank you very much!
This line:
Dim pCur As ESRI.ArcGIS.Geodatabase.ICursor = pTable.Search(Nothing, False) ' open the cursor
should be simply:
Dim pCur As ESRI.ArcGIS.Geodatabase.ICursor
I'm guessing this line:
queryFilter.WhereClause = "FID_Segmen = '" + MaxOBJECTID.ToString + "'"
should be:
queryFilter.WhereClause = "FID_Segmen = " & MaxOBJECTID.ToString
I'm assuming your field FID_Segmen is a field of type long.
Hi Duncan,
Yes you are right, I have modified the code and it works properly. Thanks a lot!
you speak about objectid and shapefile: do you mean fid in shapefile? I advise you see the differences in the behavior of the OBJECTID, FID, and OID fields because FID in shapefile hasn't same behavior of objectid: it's a offset from origin of first row so I advise you don't use for reference your data ("... If a record from a shapefile is deleted, the FIDs are renumbered so that they start from 0 and increase sequentially...") while OBJECTID in gdb is a autonumber. 37480 - What are the differences in the behavior of the OBJECTID, FID, and OID fields?
Hi Domenico,
Thanks for the reminder, I will be careful on it.