I have a query filter which works if pass a new instance of QueryFilterClass without setting any properties. But it breaks if I set the WhereClause. I get an error 0x80040358
queryFilter.WhereClause = "HouseNum = '4501'"; //this is my WhereClause which will make table.Search(queryFilter, true) break
This is my code which works:
IQueryFilter queryFilter = new QueryFilterClass();
//queryFilter.WhereClause = "HouseNum = '4501'";
IQueryFilterDefinition queryFilterDef = (IQueryFilterDefinition)queryFilter;
FileInfo file = new FileInfo(dbfPath);
String dbfDirectoryPath = file.Directory.FullName;
IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactory();
Workspace workspace = workspaceFactory.OpenFromFile(dbfDirectoryPath, 0) as Workspace;
IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;
String dbfTable = file.Name;
ITable table = featureWorkspace.OpenTable(dbfTable);
int shapeIndex = table.FindField("Shape");
try
{
using (ComReleaser comReleaser = new ComReleaser())
{
ICursor cursor = table.Search(queryFilter, true);
comReleaser.ManageLifetime(cursor);
IRow row = null;
while ((row = cursor.NextRow()) != null)
{
object o = row.get_Value(shapeIndex);
IPoint p = o as IPoint;
p.Project(gcs);
double doubleX = p.X;
double doubleY = p.Y;
}
}
}
How can I fix my WhereClause? Also does QueryFilter support the SQL LIKE Condition?
4 Answers 4
It is probably best practice to confirm that a field exists before using it in a queryfilter.
This can be done using the ITable.FindField or IFeatureclass.FindField method.
-
1+1 Validating the data model (at least on the column naming level) is a good thing to do before letting your code proceed any futher. If you do not do that, you risk getting a non-informative exception later, making the code much harder to debug.Petr Krebs– Petr Krebs2011年04月25日 21:52:11 +00:00Commented Apr 25, 2011 at 21:52
I often see people asking how to figure out what "erro x" is. So even though someone already potentially answered this question, I will still show you how decode the HRESULT value.
All you have to do is grab the hex number, interpret it as a signed 32 bit number, and then search for it on google (or the ESRI site). If it is a GDB error, it will show up as on of the value in the FDO error constant or the sdeErro constants. I just use python like so:
[email protected] ~ $ python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> i = 0x80040358
>>> s32 = (i + 2**31) % 2**32 - 2**31
>>> s32
-2147220648L
>>>
As you can see, the error is -2147220648 , if you are using a search engine, search for 2147220648 (without the minus) because search engines usually have a meaning for the minus in front.
Right away, my google search shows an fdoError constant as a result.
FDO_E_TABLE_COLUMN_NOT_FOUND -2147220648 A column was specified that does not exist.
The table you are querying does not have the field you want.
Yes it does support the LIKE. Try this:
queryFilter.WhereClause = "HouseNum LIKE '4501'";
EDIT #1:
Also you might want to try using the "serverContext.CreateObject"
http://nicogis.blogspot.com/2010/02/server-context-empty.html
EDIT #2:
Also try setting the SubFields.
queryFilter.SubFields = "HouseNum";
-
1IMO servercontext is a red herring.Kirk Kuykendall– Kirk Kuykendall2011年04月25日 18:11:23 +00:00Commented Apr 25, 2011 at 18:11
-
I believe Kirk's answer is more correct in the context of what was asked and how it was solved and should be the one marked as accepted.Petr Krebs– Petr Krebs2011年04月25日 21:48:33 +00:00Commented Apr 25, 2011 at 21:48
-
The query format and capabilities in terms of supported clauses and constructs is always determined by the underlying storage. One needs to be careful since the exact syntax for LIKE differs for, say, shapefiles and personal geodatabases. The ISQLSyntax interface provides the necessary information.Petr Krebs– Petr Krebs2011年04月25日 21:58:26 +00:00Commented Apr 25, 2011 at 21:58
Open the Select by Attributes dialog, manually create the query, and compare that syntax with your WhereClause.
-
based on earlier questions, he doesn't have desktop - just arcengine.Kirk Kuykendall– Kirk Kuykendall2011年04月25日 18:08:48 +00:00Commented Apr 25, 2011 at 18:08
HouseNum
is not a valid column name for that layer, or that the data type is an integer and not a string?