问题
My web site uses custom cultures that are created like this:
var cib = new CultureAndRegionInfoBuilder("en-gb-xxxx", CultureAndRegionModifiers.None);
cib.LoadDataFromCultureInfo(new CultureInfo("en-gb"));
cib.LoadDataFromRegionInfo(new RegionInfo("en"));
cib.Register();
And that are used like this:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-gb-xxxx");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-gb-xxxx");
It is all working fine up to the point I want to render a report with the ReportViewer control. The report text renders ok but the images fail and I get the following error:
System.Globalization.CultureNotFoundException: Culture is not supported. Parameter name: culture 4096 (0x1000) is an invalid culture identifier
After a lot a googling I found a lot of other people asking the same question, unfortunately always left unanswered. This article describes the problem very clearly but does not offer any solution.
Is there anyone who could offer if not a fix, a workaround?
回答1:
As no answer came up I had to hack my way through:
First I created a CustomReportViewer
, inheriting from the native Microsoft.Reporting.WebForms.ReportViewer
overriding the OnPreRender
method. It checks the culture of the logged user (saved in Session) and roll back to the parent culture if any.
protected override void OnPreRender(EventArgs e)
{
var culture = new CultureInfo(HttpContext.Current.Session["UserCulture"]);
if (culture.CompareInfo.LCID != culture.LCID){
var baseCulture = new CultureInfo(culture.CompareInfo.LCID);
System.Threading.Thread.CurrentThread.CurrentCulture = baseCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = baseCulture;
}
base.OnPreRender(e);
}
Then I changed the object in the aspx page:
<web:CustomReportViewer ID="ReportViewer1" runat="server" AsyncRendering="false" SizeToReportContent="true" ProcessingMode="Remote">
Et voilà
I won't accept this answer hoping someone can come up with something... cleaner
回答2:
I found the solution of the above issue:
Issue:
In Report Viewer on export Culture is not supported. Parameter name: culture 4096 (0x1000) is an invalid culture identifier.
Solution::
1.) Add below directive on the top
<% @Import Namespace="System.Globalization" %>
<% @Import Namespace="System.Threading" %>
Or
using System.Threading;
using System.Globalization;
2.) Add event as below:
protected void FPReportViewer_PreRender(object sender, EventArgs e)
{
CultureInfo ci = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
}
3.) Update Reportviewer html tag as below:
<rsweb:ReportViewer runat="server" ID="FPReportViewer" OnPreRender="FPReportViewer_PreRender" AsyncRendering="false" width="100%" SizeToReportContent="True"> </rsweb:ReportViewer>
may this code help you.
Thanks Alot!
回答3:
Try this before send to SSRS
Thread.CurrentThread.CurrentCulture =
Thread.CurrentThread.CurrentUICulture =
CultureInfo.DefaultThreadCurrentCulture =
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo("en-US");
for example:
[SessionExpire]
public ActionResult ReportProduct(string ExpenseCenter)
{
Thread.CurrentThread.CurrentCulture =
Thread.CurrentThread.CurrentUICulture =
CultureInfo.DefaultThreadCurrentCulture =
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo("en-US");
ReportViewer reportViewer = new ReportViewer();
reportViewer.ProcessingMode = ProcessingMode.Remote;
reportViewer.SizeToReportContent = true;
reportViewer.AsyncRendering = false;
reportViewer.ZoomMode = ZoomMode.FullPage;
reportViewer.Width = Unit.Percentage(100);
reportViewer.Height = Unit.Percentage(100);
var serverReport = reportViewer.ServerReport;
serverReport.ReportPath = "/Reports/ReportPrice";
serverReport.ReportServerUrl = new Uri("http://ssrs-server:94/ReportServer_MSSQLSERVER2016/");
serverReport.SetParameters(GetParametersServer());
serverReport.Refresh();
ViewBag.ReportViewer = reportViewer;
return View();
}
来源:https://stackoverflow.com/questions/27404755/using-custom-culture-with-reportviewer-returns-a-culturenotfoundexception