Thursday, January 02, 2014

#Error in local report parameter value and expressions in Visual Studio 2010

I had faced this issue when i upgraded my VS2008 RDLC reports in VS2010 to adopt 2010 Schema (another night mare :)). I was using a local reprot to render in PDF format in my web apps and it consumes certein paramters to show in header, footer and also used in some of the expressions. Unfortunately, all my textboxes that used this paramter value displayed "#Error". I had spent almost around 3 hours on this and later realized the cause. This is due to some CAS (Code Access Security) recently implemented in .net 4.0 framework. We need to give additional permission to evaluate the expression in RDLC report. Details are mentioned here in the blog nicely.

Here is the sample code of mine. The object "report" represents local report object (LocalReport report = new LocalReport();)


ReportParameter[] parameters = new ReportParameter[4];
ReportParameter p2 = new ReportParameter("datetime_format", SessionContext.CurrentSessionDateTimeFormat);
ReportParameter p3 = new ReportParameter("date_format", SessionContext.CurrentSessionDateFormat);
ReportParameter p4 = new ReportParameter("currency", "All transactions are in " + SessionContext.CurrencyCode);
//
parameters[0] = new ReportParameter("request_no", requestNo);
parameters[1] = new ReportParameter("datetime_format", SessionContext.CurrentSessionDateTimeFormat);
parameters[2] = new ReportParameter("date_format", SessionContext.CurrentSessionDateFormat);
parameters[3] = new ReportParameter("currency", "All transactions are in " + SessionContext.CurrencyCode);
//
report.SetBasePermissionsForSandboxAppDomain(AppDomain.CurrentDomain.PermissionSet.Copy());
// Added above line to grant additional permission to execute the expression assembly
//
report.SetParameters(parameters);


Cheers...

Note: The above fix is required for the expressions/formulas to work properly in the new version.

1 comment:

Marimuthu said...

Thanks a lot. You saved me from the hell