ThinkGeo.com    |     Documentation    |     Premium Support

Odd error with layer.ExecuteQuery

Below you will find some simple code to execute a query from a shapefilefeaturesource. Please substitute with your data and try it out. An exception of "IErrorInfo.GetDescription failed with E_FAIL(0x80004005)" should appear on the ExecuteQuery line. If not, please inform me and I can perhaps supply my layer and we can verify what the exception is and why it is happening.............


 


 


        Dim strSQL As String
        Dim strColumnName As String = "ZONE"
        Dim layerPath As String = "C:\Program Files\geotms\maps\lots.shp"
        Dim layerName As String
        Dim dt As New DataTable

        If strColumnName <> Nothing Then
            Dim layerToAddSHP As New ShapeFileFeatureLayer(layerPath, ShapeFileReadWriteMode.ReadOnly)

            layerName = System.IO.Path.GetFileNameWithoutExtension(layerPath)

            strSQL = "SELECT " & strColumnName & _
                    " FROM " & layerName & _
                    " GROUP BY " & strColumnName

            layerToAddSHP.RequireIndex = False

            Try
                layerToAddSHP.Open()
                dt = layerToAddSHP.QueryTools.ExecuteQuery(strSQL)

            Finally
                If layerToAddSHP.IsOpen Then layerToAddSHP.Close()
            End Try

        End If

    End Sub


Nelson,


It was suspicious that the magic is hidden in your shape file, so if possible can u send me the shape file “lots.shp”.
 
I use the following codes with a shape file attached in sample data, it works fine.

  Private Sub SqlQuery_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            winformsMap1.MapUnit = GeographyUnit.DecimalDegree
            winformsMap1.BackgroundOverlay.BackgroundBrush = New GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean)

            Dim worldLayer As New ShapeFileFeatureLayer("..\..\SampleData\Data\USStates.shp")
            worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.State1
            worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.OutlinePen.LineJoin = DrawingLineJoin.Round
            worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20

            Dim staticOverlay As New LayerOverlay()
            staticOverlay.Layers.Add("WorldLayer", worldLayer)
            winformsMap1.Overlays.Add(staticOverlay)

            winformsMap1.CurrentExtent = New RectangleShape(-178.0R, 71.5, -67.0R, 19.0R)
            winformsMap1.Refresh()
        End Sub

        Private Sub btnExecute_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim worldLayer As ShapeFileFeatureLayer = DirectCast(winformsMap1.FindFeatureLayer("WorldLayer"), ShapeFileFeatureLayer)

            Dim strColumnName As String = "PERIMETER"
            Dim layerName As String = System.IO.Path.GetFileNameWithoutExtension(worldLayer.ShapePathFileName)

            Dim sqlStatement As String = "SELECT " & strColumnName & _
                    " FROM " & layerName & _
                    " GROUP BY " & strColumnName

            worldLayer.RequireIndex = False
            worldLayer.Open()
            Dim dataTable As DataTable = worldLayer.QueryTools.ExecuteQuery(sqlStatement)
            worldLayer.Close()

            dgridResult.DataSource = dataTable
        End Sub

Let me know if still any more questions.


Thanks.
 
Yale

Unfortunately, that may be the case. It is strange because it has been working fine all this time. My layer is too large to upload unfortunately so I will email support and request a forward to you with a link to this post.



Nelson,


Thanks for your post!
 
I think the reason is that ZONE is the reserved keywords that used by SQL statement. You can find it below:
developer.mimer.com/validato...-words.tml
 
One way to correct this to add the bracket around this word
Select [ZONE] From LOTS
 
So, just change the following statement:

Dim strColumnName As String = "[ZONE]"

Any more questions just let me know.


Thanks.
 
Yale

Yale, when placing brackets around my columnName, I get the following error: 
  
 Column ‘[ZONE]’ does not belong to table.

Nelson,


That is strange!
 
Can you try to do the sql statement test on MapSuiteExplorer as following screenshot shows

 
 
Let me know if you have any more questions.
 
Thanks.
 
Yale

 



The error at this point is my fault. I tried this code in a different part of my project and it worked correctly.


The problem I had been running into was a result of checking for DBNull against the column name including the brackets which you can not include once the data is packed into the DataTable.


Thank you, Yale.



Nelson, You are welcome! 
  
 Any more questions just let me know! 
  
 Thanks. 
  
 Yale