We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Dynamic columns in ASP.NET MVC Grid with string property containing Json

I have a table that I am reading from a database and one of the columns contains Json Data all of which I am passing to the Grid. The content displays in the Grid. However, I receive the following error message when trying to export the Grid to Excel:

DescriptionAn unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception DetailsSystem.NullReferenceException: Object reference not set to an instance of an object.

Some of the relevant code:

public class GridRow
{    
        public string Symbol   get; set; }
        
        [Display(Name = "Entered Date")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yy HH:mm:ss}")]
        public DateTime Date { get; set; }

        [Display(Name = "Entered Quantity")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:n0}")]
        public int Quantity { get; set; }

        // I am using JSON.NET and created a custom converter that will pass the Json text as is without any encoding
        [JsonConverter(typeof(JsonStringJsonConverter))]
        public string LogInformation { get; set; }
}

Html.EJ().Grid<GridRow>("TradesGrid").AllowFiltering().AllowPaging().AllowSorting().AllowTextWrap().HtmlAttributes(new Dictionary<string, object> { { " data-export-excel-url", Url.ExportTradesToExcel() } }).ShowColumnChooser().FilterSettings(filter => { filter.FilterType(FilterType.Excel); }).PageSettings(pg => pg.PageSize(10)).Columns(col =>
{
    col.Field(r => r.Date).Add();
    col.Field(r => r.Symbol).Template("<a class='symbol'>{{:Symbol}}</a>").Add();
    col.Field(r => r.Quantity).TextAlign(TextAlign.Right).Add();
    col.Field("LogInformation.BoundedPct").TextAlign(TextAlign.Right).Add();
    col.Field("LogInformation.TermStructure").Add();
})

The grid's data is being set through Ajax - and the data is used in multiple other areas on the page:

function refreshTradesGrid(backtestResultId) {
$.ajax({
type: "GET",
dataType: "json",
url: settings.getTradesUrl,
data: {
backtestResultId: backtestResultId
},
success: function(result) {
var jsonResult = ej.parseJSON(result);
settings.$tradesGrid.ejGrid("dataSource", jsonResult);
// use the trade data elsewhere on the page
refreshTradesPivotGrid(jsonResult, false);
refreshHistogramChart(jsonResult);
    }});
}


Full stack trace of the exception:

[NullReferenceException: Object reference not set to an instance of an object.]
Syncfusion.EJ.Export.GridExcelExport.ProcessRecordCell(Object row, Column column) +342
Syncfusion.EJ.Export.<>c__DisplayClass10.<ProcessRecordRow>b__f(Column column) +206
System.Collections.Generic.List`1.ForEach(Action`1 action) +85
Syncfusion.EJ.Export.GridExcelExport.ProcessRecordRow(Object row) +162
Syncfusion.EJ.Export.GridExcelExport.RenderRecord() +105
Syncfusion.EJ.Export.GridExcelExport.ProcessGridContents() +139
Syncfusion.EJ.Export.GridExcelExport.IterateElements() +315
Syncfusion.EJ.Export.GridExcelExport.ExportHandler() +13
Syncfusion.EJ.Export.GridExcelExport.ExecuteResult(GridProperties GridModel, Object dataSource) +418
Syncfusion.EJ.Export.GridExcelExport.ExportHelper(GridProperties gridModel, Object dataSource) +599
Syncfusion.EJ.Export.GridExcelExport.Export(GridProperties gridModel, Object dataSource, Boolean multipleExport) +18
Syncfusion.EJ.Export.ExcelExport.Export(GridProperties gridmaodel, Object datasource, String excelname, ExcelVersion excelversion, Boolean isHideColumnIncude, Boolean isTemplateColumnIclude, String theme) +101
Syncfusion.EJ.Export.ExcelExport.Export(GridProperties gridmaodel, IEnumerable datasource, String excelname, ExcelVersion excelversion, Boolean isHideColumnIncude, Boolean isTemplateColumnIclude, String theme) +30
JMS.EuclidWeb.Shared.ExportSyncFusionGrid.ExportToExcel(IEnumerable rows, GridProperties gridProperties, String fileName) in D:\Dropbox\Source\Euclid\Prod\EuclidWeb\Shared\ExportSyncFusionGrid.cs:134
JMS.EuclidWeb.Controllers.<ExportExitTradesToExcel>d__20.MoveNext() in D:\Dropbox\Source\Euclid\Prod\EuclidWeb\Controllers\BacktestResultController.cs:453
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +97
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +17
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +225
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +129

Regards, Jeff

1 Reply

SA Saravanan Arunachalam Syncfusion Team March 24, 2017 05:00 AM UTC

Hi Jeffrey, 
Thanks for contacting Syncfusion’s support. 
We have already discussed this query in the following knowledge base document. 
Regards, 
Saravanan A. 


Loader.
Up arrow icon