C# – A circular reference was detected while serializing an object of type ‘System.Reflection.RuntimeModule’

asp.net-mvccjqueryjson

The below code will read oracle table and return json result, to the view.
The data are loaded successfully into the datalist, however the view is throwing the below error:

A circular reference was detected while serializing
an object of type 'System.Reflection.RuntimeModule'.

Q1: What I can do to fix the error? I already have a model defined for the data I need to retrieve (see the below model definition)

Q2: Is the above method is the best practice to read a database and return json?

public JsonResult StudList()
{
    string SQL = "select id, name, div_code, block, from students where ....."; //see the below model 
    var con = DB.GetConnection();
    con.Open();
    OracleDataAdapter oraAdapt = new OracleDataAdapter(SQL, con);
    DataTable dt = new DataTable();
    oraAdapt.Fill(dt);
    con.Close();
    con.Dispose();
    List<DataRow> dtList = dt.AsEnumerable().ToList(); 
    return Json(dtList, JsonRequestBehavior.AllowGet); 
}

Model:

public class GetSDetailsModel
{
    public List<GetStudentSearchModel> GetStudentSearchModel { get; set; }
}


public class GetStudentSearchModel
{
    public string id { get; set; }
    public string name { get; set; }
    public string div_code { get; set; }
    public string level_code { get; set; }
    public string program_code { get; set; }
    public string major_code { get; set; }
    public string PGPA { get; set; }     

}

Best Answer

List<Dictionary<string, object>> arrResponse = new List<Dictionary<string, object>>();
DataTable reader = db.executeQueryDataTable(sSQL);
foreach (DataRow row in reader.Rows)
{
    Dictionary<string, object> dictRow = new Dictionary<string, object>();
    foreach(DataColumn col in reader.Columns)
        dictRow[col.ColumnName] = row[col.ColumnName];
    arrResponse.Add(dictRow);

}

the above code turns the DataTable into a serializable List of Dictionary. just serialize arrResponse then