gustavnavar / Grid.Blazor

Grid component with CRUD for Blazor (client-side and server-side) and ASP.NET Core MVC
GNU Lesser General Public License v2.1
699 stars 135 forks source link

Exporting a grid crashes (for me) in latest version 5.0.12 #416

Open miszelek opened 4 months ago

miszelek commented 4 months ago

After upgrading to 5.0.12 (from 5.0.11) exporting a grid fails with the following error:

[2024-05-30T13:21:44.621Z] Error: System.ArgumentNullException: Value cannot be null. (Parameter 'source')
   at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
   at System.Linq.Enumerable.Where[TSource](IEnumerable`1 source, Func`2 predicate)
   at GridBlazor.ExcelCell.RemoveInvalidXmlChars(String content)
   at GridBlazor.ExcelCell.set_Content(String value)
   at GridBlazor.ExcelCell..ctor(String value, Type type)
   at GridBlazor.ExcelWriter.GenerateExcel[T](IGridColumnCollection`1 columns, IEnumerable`1 items)
   at GridBlazor.CGrid`1.DownloadExcel(IJSRuntime js, String filename)
   at GridBlazor.Pages.GridComponent`1.ExcelHandler()
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)

To Reproduce I am trying to make a small repro which shows the issue, but did not succeed yet. But my project has this issue in version 5.0.12, but export works normally in 5.0.11 and below.

Looking into the GridBlazor code changes for 5.0.12 I wonder if the change (RemoveAndInvalidXmlChars) takes into account when the given string is null. Calling LINQ Where() on a null value also provides an exception.

Expected behavior Exporting a grid should result in a Excel file (and not an exception).

Possible Fix Adding a conditional null operator (?) on content variable prevents this.

private string RemoveInvalidXmlChars(string content)
{
  return new string(content?.Where(ch => System.Xml.XmlConvert.IsXmlChar(ch)).ToArray());
}

Desktop:

gustavnavar commented 4 months ago

I've published a new package version with your modification

miszelek commented 4 months ago

Thanks for the quick fix! Updated to the new package and Export is working again.

Btw, I love GridBlazor, keep up the good work!