This project has moved and is read-only. For the latest updates, please go here.

KeyNotFoundException

Jun 22, 2011 at 4:29 PM

Running some reports I get this exception and not sure why. I am passing in a List<T> to ExcelReportWriter.WriteReport()

 

[KeyNotFoundException: The given key was not present in the dictionary.]
   System.Collections.Generic.Dictionary`2.get_Item(TKey key) +9619709
   Doddle.Reporting.FieldDataDictionary.GetFormattedString(RowField field) +21
   Doddle.Reporting.Writers.HtmlReportWriter.RenderRow(ReportRow row, RenderHintsCollection hints) +419
   Doddle.Reporting.Writers.HtmlReportWriter.BuildReportHtml(ReportTextFieldCollection textFields, RenderHintsCollection hints, ReportRowCollection rows) +248
   Doddle.Reporting.Writers.HtmlReportWriter.WriteReport(Report report, Stream destination) +62
   MRMPortal.MVC.Areas.Reports.Controllers.ReportsController.ModalityCompliance(ModalityComplianceViewModel reportParameters) in C:\Users\jjorczak\Documents\Visual Studio 2010\Projects\MRMPortal\Branches\Development\Version_1.11\MRMPortal.MVC\Areas\Reports\Controllers\ReportsController.cs:116
   lambda_method(Closure , ControllerBase , Object[] ) +108
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8920029
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

Jun 22, 2011 at 8:26 PM

I think this is related to strings that are null, but I am not sure why it would not support that as this is a common scenario for a report.

Jun 22, 2011 at 8:32 PM

Hi jjorczak,

Can you possibly paste the code that you are using the generate the report? Given you think it has to do with null strings could you try verifying that real quick. I also assume you're using the latest compiled version, or the latest version from source code? I will try and looking it tonight, but if you could provide as much detail as possible that would help me out.

-Matt

Jun 22, 2011 at 9:04 PM

Container class:

        public class ReportData
        {
            public int Id { get; set; }
            public string LastName { get; set; }
            public string FirstName { get; set; }
        }

And a LINQ expression to get data:

     var reportData = from users in _context.Users

                                 select new ReportData
                                                {
                                                    Id = users.Id,
                                                    LastName = users.LastName,
                                                    FirstName = users.FirstName,
                                                };

If LastName or FirstName is null, it bombs.

If I force it with nulls, it bombs.

var reportData = from users in _context.Users

                            select new ReportData
                                                {
                                                    Id = 1,
                                                    LastName = null,
                                                    FirstName = null,
                                                };

Jun 23, 2011 at 1:28 AM
Edited Jun 23, 2011 at 1:51 PM

Here is a more concise example that produces the error:

        public class ReportData
        {
            public int Id { get; set; }
            public string LastName { get; set; }
            public string FirstName { get; set; }
        }

        public ReportResult MyReport()
        {
            var reportData = new List<ReportData>();
            reportData.Add(new ReportData() { FirstName = "a", Id = 1, LastName = "A" });
            reportData.Add(new ReportData() { FirstName = "b", Id = 2, LastName = "B" });
            reportData.Add(new ReportData() { FirstName = null, Id = 3, LastName = "C" });
            var report = new Report(reportData.ToReportSource());
            return new ReportResult(report);
        }

The 3rd data element (row) is what produces the error because of the null string value.

Jun 23, 2011 at 1:39 PM
Edited Jun 23, 2011 at 1:51 PM

Ok, I downloaded the source code and stepped through this problem. The issue is occurring in ReportRow.cs line 19:

      _fieldData[rowField] = source.GetFieldValue(dataItem, field.Name);

If the dataItem for the field.Name is null, the "_fieldData[rowField]" is removed from the dictionary so that later we get the error when the value is attempted to be retrieved.

I added two lines to the EnumerableReportSource.GetFieldValue method to trap a null value and return stringEmpty as you do for a null dataItem. This fixed the issue:

        public object GetFieldValue(object dataItem, string fieldName)
        {
            if (dataItem == null)
                return string.Empty;

            var value = dataItem.GetProperty<object>(fieldName);

            // NEW: convert a null value into an empty string so the dictionary has a value
            if (value == null)
                return string.Empty;

            return value;
        }

Oct 12, 2011 at 10:40 AM

I have the same issue, but i have no null fields. Only strings, ints and bools...
What sohuuld i do?

Oct 12, 2011 at 2:14 PM
I ended up taking a copy of the source code and fixing the issue.

Sent from my Windows Phone

From: XVincentX
Sent: 10/12/2011 5:41 AM
To: jeff@jorczak.com
Subject: Re: KeyNotFoundException [doddlereport:262359]

From: XVincentX

I have the same issue, but i have no null fields. Only strings, ints and bools...
What sohuuld i do?

Oct 12, 2011 at 2:23 PM

You were right: i had null values.
since i do not want to modify source code, i made a cycle and set string.Empty if value is null.
Thank you a lot.

Oct 12, 2011 at 2:41 PM

Sorry for the delays guys. I think I'm going to convert this project to mercurial so people can more easily submit pull requests (aka, fix the code, and check it in) so that I won't have to manually fix bugs in the future. I don't want to be the bottleneck if I can't find enough time!

In the mean time, let me look into this and try and get a fix in the NuGet package by this weekend

-Matt

Oct 12, 2011 at 2:42 PM

Good idea, I might use that…

From: XVincentX [email removed]
Sent: Wednesday, October 12, 2011 9:24 AM
To: jeff@jorczak.com
Subject: Re: KeyNotFoundException [doddlereport:262359]

From: XVincentX

You were right: i had null values.
since i do not want to modify source code, i made a cycle and set string.Empty if value is null.
Thank you a lot.

Oct 15, 2011 at 1:26 PM

Hello Im new to use Doddle reports. So Im not getting how to generate report. I tried the code provided in the site, but unable to generate.

Can any one help me to generate report in web application or send me one example project.

And importantly im confusing with .ashx file and .dbml (which code has to be written in which file).

Help please 

Nov 28, 2011 at 6:19 PM

Hey guys, a couple updates:

1) The source code is not stored in Mercurial, so if we ever need patches in the future it will be much easier to fork it and create a pull request

2) I've updated the source with this fix - I actually just added the null check in the ReportRow.cs so that it will work with all IReportSources, hopefully it's all good now.

If you get a second to verify and let me know, please do! I will be updating the NuGet packages sometime soon.