Add Layer from Geodatabase not working

Hi all, it might old but still struggling from it.
I have an ESRI geo-database that has Roads data. I want to display it in axMap control.
This is my function:

private void openGDB()
{
    try
    {
        var ogr = new OgrDatasource();
        string source = @"path\data.gdb";
        if (!ogr.Open(source)) // no error here
        {
            MessageBox.Show("Failed to open data source. Err: " + ogr.GdalLastErrorMsg);
        }
        else
        {
            int count = ogr.LayerCount; // This displays count properly
            Debug.WriteLine(count);

            var layer = new OgrLayer();
            layer = ogr.GetLayerByName("Road");

            int a = axMap1.AddLayer(layer, true) ; // here a gets number 1
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

Code works without error but data are not displayed.
When I debug the code, the only error I get is the following at layer var:
GdalLastErrorMsg = “Unable to open EPSG support file gcs.csv. Try setting the GDAL_DATA environment variable to point to the directory containing EPSG csv files.” when layer created. But when I check layer’s values again in the same run, that error disappears.
When I add shapefile to axMap it works fins, but from geo-database it does not. All layers has the same projection.
Also as long as I remember, while I was installing mapwingis, I was asked while installation if I want to register variables to system, and I selected yes to that. So, GdalLastErrorMsg kind of make no sense to me!
Can someone enlighten me on what is wrong with my code.

Hello @alrajhi20

First try something more generic, like

for (int i = 0; i < count; i++)
{
    var layer = ogr.GetLayer(i);
    Debug.Print("Layer name: " + layer.Name);
    int a = axMap1.AddLayer(layer, true);
}

Another option is if you have MapWindow5 installed (which uses the OCX), you could try loading it there.

If neither of these options reveal anything new, would you mind posting the GDB and I can debug it on this end?

Regards,
Jerry.

Dear Mr. Jerry, Thank u for your reply. I added the code and all layers got printed successfully. But it does not appear in the axMap. The other options you suggested, I installed mapwindow5, but when I navigate to any esri geo-database, it only enables me to select geo-database not feature class; not expandable.
See attached picture.
mwg
It shows all shapefiles properly. Do you think mapwindow5 has same issue in my machine as mine?
Please advise, Thanks

Can you have a look if the GDAL_DATA variable is correctly set on your machine?
It should point to the gdal-data subfolder of the folder MapWinGIS is installed.
If it points to the correct location can you check if the file gcs.csv is in it?

Hi Mr. Pmeems,
I checked GDAL_DATA variable but wasnt there. I added it with the proper folder where mapwingis is installed. I also found gcs.csv in that folder. But still data not shown.

gcs

var

Can you share (part of) your gdb file? So we can debug?

Yes Mr. pmeems,
Please have a look to gdb attached. I sent sample data.
data.zip (2.0 MB)

I added this code behind a button in my test application:

private void btnLoadGdb_Click(object sender, EventArgs e)
{
    var ogr = new OgrDatasource();
    var utils = new Utils();

    try
    {
        axMap1.Clear();

        const string source = @"path\data.gdb";
        if (!ogr.Open(source)) 
        {
            txtResults.Text += $@"{Environment.NewLine}Failed to open data source. Err: {ogr.GdalLastErrorMsg}";
        }
        else
        {
            var count = ogr.LayerCount; 
            txtResults.Text += $@"{Environment.NewLine}Number of layers: {count}";

            for (var i = 0; i < count; i++)
            {
                var layer = ogr.GetLayer(i);
                txtResults.Text += $@"{Environment.NewLine}Adding layer name: {layer.Name}{Environment.NewLine}";
                axMap1.AddLayer(layer, true);

                // Set some coloring:
                var buffer = layer.GetBuffer();

                switch (layer.ShapeType2D)
                {
                    case ShpfileType.SHP_POINT:
                        buffer.DefaultDrawingOptions.FillColor = utils.ColorByName(tkMapColor.Red);
                        buffer.DefaultDrawingOptions.LineColor = utils.ColorByName(tkMapColor.DarkRed);
                        break;
                    case ShpfileType.SHP_POLYLINE:
                        buffer.DefaultDrawingOptions.FillColor = utils.ColorByName(tkMapColor.Green);
                        buffer.DefaultDrawingOptions.LineColor = utils.ColorByName(tkMapColor.DarkGreen);
                        break;
                    case ShpfileType.SHP_POLYGON:
                        buffer.DefaultDrawingOptions.FillColor = utils.ColorByName(tkMapColor.Blue);
                        buffer.DefaultDrawingOptions.LineColor = utils.ColorByName(tkMapColor.BlueViolet);
                        buffer.DefaultDrawingOptions.FillTransparency = 200;
                        break;
                }
            }
        }

        var numLayers = axMap1.NumLayers;
        txtResults.Text += $@"{Environment.NewLine}Number of loaded layers: {numLayers}";

        // Change order of layers: points on top, polygons at the bottom:
        for (var i = 0; i < numLayers; i++)
        {
            var layerHandle = axMap1.get_LayerHandle(i);
            var sf = axMap1.get_Shapefile(layerHandle);
            switch (sf.ShapefileType2D)
            {
                case ShpfileType.SHP_POINT:
                    axMap1.MoveLayerTop(layerHandle);
                    break;
                case ShpfileType.SHP_POLYGON:
                    axMap1.MoveLayerBottom(layerHandle);
                    break;
            }
        }
    }
    catch (Exception ex)
    {
        txtResults.Text += $@"{Environment.NewLine}Exception: {ex}";
        txtResults.Text += $@"{Environment.NewLine}OgrDatasource error: {ogr.GdalLastErrorMsg}";
    }
}

And the result is:

The logging says:

Number of layers: 3
Adding layer name: Enclosure 
4 Converting geometries...8 Converting geometries...13 Converting geometries...17 Converting geometries...21 Converting geometries... [...] ...100 Converting geometries...100 Completed Error: GDAL FAILURE: SELECT from table mw_styles failed, no such table/featureclass.
Adding layer name: BuiltUpArea 
6 Converting geometries...12 Converting geometries...18 Converting geometries...25 Converting geometries...31 Converting geometries...[...] ...100 Converting geometries...100 Completed Error: GDAL FAILURE: SELECT from table mw_styles failed, no such table/featureclass.
Adding layer name: Road 
1 Converting geometries...2 Converting geometries...3 Converting geometries...4 Converting geometries...5 Converting geometries...6 Converting geometries...[...] ...100 Converting geometries...100 Completed Error: GDAL FAILURE: SELECT from table mw_styles failed, no such table/featureclass.
Number of loaded layers: 3

The FAILURE message is not a problem. That table is used to style the data and is optional.

It looks OK to me

This works perfect. Thank you Mr. pmeems, you really saved me a lot of time. Very beautiful.
Though having it working, I tried to modify my code to match yours, but it never works with other than your code!!!.
Any way, Thank again