0

I want to bind my C# datatable to the jQuery datatable. However a popup appears that says

DataTables warning: table id=extracionTable - Ajax error. For more information about this error, please see http://datatables.net/tn/7

and the table says "Loading..." but does nothing. I don't understand why this is the case.

Here is my controller.

public JsonResult loadExtraccionTable(string table_name)
{
    table_name = "ext_clientes";
    MetaViewContext metaViewContext = new MetaViewContext();
    return Json(new { data = metaViewContext.GetMetaView(table_name).table.AsEnumerable().ToList() }, JsonRequestBehavior.AllowGet);
}

This part of the code returns a Datatable

 metaViewContext.GetMetaView(table_name).table

I added the .AsEnumerable().ToList() because I thought it would make a difference

Here is how I make my c# datatable

public DataTable simpleExportQuery(string p_table)
{

    string selectStatement = "select * from " + p_table;
    DataTable dt = new DataTable();
    using (MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connString))
    {
        conn.Open();
        using (MySqlCommand cmd = conn.CreateCommand())
        {
            cmd.CommandText = selectStatement;
            using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
            {
                sda.Fill(dt);
            }
        }
    }

    return dt;
}

Here is my script

<script>
$(document).ready(function () {
    var table = $('#extracionTable').DataTable({
        "ajax": {
        "url": "/extraccion/loadExtraccionTable",
        "type": "GET",
        "datatype": "json",
        },
        rowReorder: {
            selector: 'td:nth-child(2)'
        },
        responsive: true,
        dom: 'Bfrtip',
        buttons: [
            'copyHtml5',
            'excelHtml5',
            'csvHtml5',
            'pdfHtml5'
        ],
        "deferRender": true,
        "columns": [
            @foreach (var item in Model.lstColumns)
            {
                <text>
                    { "data":"@(item.columnName)", "autoWidth": true },
                </text>
             }
        ],
    });
});
</script>

Here is what I assume my json looks like

[
   {
      "cliente_id":2,
      "nombre_cliente":"my name",
      "direccion":"some adresss",
      "cuidad":"city",
      "departamento":"agricolture",
      "codigo_postal":"444444",
      "pais_nombre":"Colombia",
      "correo_facturacion_electronica":"True",
      "DANE":3,
      "digito_verificacion":4444,
      "NIT":3,
      "plazo_pago":0,
      "comercial_asignado":"some name",
      "con_potencial":1,
      "confirmado":1,
      "eliminado":0,
      "fecha_creacion":"2017-12-22T00:00:00",
      "visitado":1,
      "activo":1,
      "comentario":"Some comment",
      "contacto_id":15,
      "nombre_contacto":"somename",
      "apellido_contacto":"name",
      "carga_contacto":"Sales",
      "correo_contacto":"cliente@email.ca",
      "telefono_fijo_contacto":"444444444",
      "telefono_movil_contacto":"444444444",
      "principal_contacto":1,
      "contacto_eliminado":false
   }
]

Also maybe this is useful I also found this error in chrome " A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule' "

Any help would be very appreciated thank you.

J.C
  • 632
  • 1
  • 10
  • 41
  • 1
    First there is no need of using AsEnumerable() as you are already using ToList(). Another thing is please try to check response in browser and paste JSON in comments. So we can have better idea – Jigar Sangoi Apr 05 '20 at 12:22
  • @JigarSangoi I can add the json in my question also maybe this is useful in chrome I have a erreur 500 that says the following A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'. – J.C Apr 05 '20 at 12:50
  • Circular reference error comes when you have relationship between tables with EF. so you have to create model for this particular entity and fill data then return in JSON. – Jigar Sangoi Apr 05 '20 at 13:01
  • 1
    How do you send column names ? From 1st action you send list as column names ? – Jigar Sangoi Apr 05 '20 at 13:22
  • @JigarSangoi I have added the code that shows you how I make my datatable – J.C Apr 05 '20 at 13:29
  • I think first you try above things then tell me what you get – Jigar Sangoi Apr 05 '20 at 13:31
  • as stated in [the link in your question](http://datatables.net/tn/7), *DataTables warning: table id=extracionTable - Ajax error.* is shown when *the server responds with anything other than a valid HTTP 2xx response*, which is exactly what's happening: it's showing you error *A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'* which is an HTTP 500. google about that error, there are also several questions in SO about it like [this](https://stackoverflow.com/q/14592781/4770813) and [this](https://stackoverflow.com/q/1153385/4770813) – Scaramouche Apr 05 '20 at 23:47

0 Answers0