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.
Unfortunately, activation email could not send to your email. Please try again.

NullReferenceException on exporting Grid with dynamically columns

Thread ID:

Created:

Updated:

Platform:

Replies:

123852 Apr 26,2016 01:17 PM Apr 27,2016 01:19 AM ASP.NET MVC 1
loading
Tags: Grid
André Secco
Asked On April 26, 2016 01:17 PM

My code has a Grid, which has dynamic columns (created in the controller). Follows the controller code and view.

Controller:

public ViewResult GridFeatures(){

            var DataSource = new NorthwindDataContext().OrdersViews.ToList();
            ViewBag.datasource = DataSource;
            
            List<Column> columns = new List<Column>();
            columns.Add(new Column() { Field = "OrderID", HeaderText = "OrderID", Width = 70 });
            columns.Add(new Column() { Field = "CustomerID", HeaderText = "CustomerID", Width = 70 });
            columns.Add(new Column() { Field = "EmployeeID", HeaderText = "EmployeeID", Width = 70 });
            columns.Add(new Column() { Field = "Freight", HeaderText = "Freight", Width = 70 });
            ViewBag.Columns = columns;

            return View();
}

public void ExportToExcel(string GridModel)
{
            ExcelExport exp = new ExcelExport();
            var DataSource = new NorthwindDataContext().OrdersViews.ToList();
            GridProperties obj = ConvertGridObject(GridModel);
            exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");
}
private GridProperties ConvertGridObject(string gridProperty)
{
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            IEnumerable div = (IEnumerable)serializer.Deserialize(gridProperty, typeof(IEnumerable));
            GridProperties gridProp = new GridProperties();
            foreach (KeyValuePair<string, object> ds in div)
            {
                var property = gridProp.GetType().GetProperty(ds.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                if (property != null)
                {
                    Type type = property.PropertyType;
                    string serialize = serializer.Serialize(ds.Value);
                    object value = serializer.Deserialize(serialize, type);
                    property.SetValue(gridProp, value, null);
                }
            }
            return gridProp;
}

View (Only the part where I set the Grid):

           @(Html.EJ().Grid<object>("Grid")
            .Locale("pt-BR")
            .Datasource((IEnumerable<object>)ViewBag.DataSource)
            .ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items =>
            {
            items.AddTool(ToolBarItems.ExcelExport);
            }))
            .AllowFiltering()
            .FilterSettings(filter => { filter.FilterType(FilterType.Excel); })
            .AllowSorting()
            .AllowGrouping()
            .SelectionType(SelectionType.Multiple)
            .AllowResizing()
            .Columns(ViewBag.Columns))


When trying to export reports in Excel, Word or PDF an error occurred:

Object reference not set to an instance of an object.
Linha 28:                     ExcelExport exp = new ExcelExport();
Linha 29:                     var DataSource = new NorthwindDataContext().OrdersViews.ToList();
Linha 30:                     GridProperties obj = ConvertGridObject(GridModel);
Linha 31:                     exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");
Linha 32:             }

Stack trace:

[NullReferenceException: Object reference not set to an instance of an object.]
Syncfusion.EJ.Export.GridExcelExport.ProcessRecordCell(Object row, Column column) +1037 Syncfusion.EJ.Export.<>c__DisplayClasse.<ProcessRecordRow>b__d(Column column) +464 System.Collections.Generic.List`1.ForEach(Action`1 action) +11439282 Syncfusion.EJ.Export.GridExcelExport.ProcessRecordRow(Object row) +282

What might be happening?

Thanks.












Balaji Marimuthu [Syncfusion]
Replied On April 27, 2016 01:19 AM

Hi André, 
Thanks for contacting Syncfusion support. 
We have created a sample and checked the issue by using the provided code snippets, but we were unable to reproduce the reported issue. Refer to the sample in following link. 
The mentioned issue is occurred, due to when the columns bound to the Grid are not defined in the Grid dataSource. We have already discussed the same issue in following knowledge base doucument. 
For example if the column field name “Employee1” is not defined in Grid dataSource, the null exception will be thrown while exporting the Grid dataSource. So, please ensure the columns bound to the Grid are defined in the Grid dataSource before exporting the Grid. Still if you face the issue, kindly share the following information. 
1.     Scenario to reproduce the issue. 
2.     Essential studio and browser version details. 
3.     Share sample if possible or modify the attached sample as issue reproducible. 
 
Regards, 
Balaji Marimuthu 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;