ASP.NET – How to set culture in web.config to change regional settings

In ASP.NET, if you get messages similar to the following:

* String was not recognized as a valid DateTime
* The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

It is feasible that the webserver and/or Db is running on different regional culture sets.

To fix this,add a globalization section to the Web.config file, and then set the uiculture and culture attributes, as shown in the following example:

 <globalization uiCulture="en" culture="en-NZ" />

The full list of cultures can be found here:
https://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.110).aspx

MVC error: “System.Web.WebPages. Razor.Configuration. HostSection cannot be cast…”

Got this error when doing a partial deployment to an MVC ASP.Net website. This one took me a while to figure out as the some of the changes in the code was done by someone working with an earlier version of Visual Studio (while I was working with 2015)..

[A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to [B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from ‘System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ in the context ‘Default’ at location ‘C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll’.

Took a while to find out the root cause. Apparently this is due to incorrect versions specified in the web.config in the VIEWS folder (not just the root folder).

Simply replace the configsection in there to reflect the new version:

 <configSections>
 <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
 <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
 <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
 </sectionGroup>
 </configSections>

Hope that helps!

ASP.Net Tip – Open a PDF File in a new window

To Open a PDF file in a new window in ASP.Net:

Html:

 <asp:LinkButton ID="lnkView" runat="server" Text="View File" OnClick="View" CommandArgument="MyFileName"></asp:LinkButton>

(The CommandArgument holds the name of the file – easier ways to pass a filename across, but this is just one of them..)

On the page where the above button is:

 protected void View(object sender, EventArgs e)
        {
            string url = string.Format("ViewPDF.aspx?fileName={0}.pdf", (sender as LinkButton).CommandArgument);  //change name of aspx as required.
            string script = "<script type='text/javascript'>window.open('" + url + "')</script>";
            this.ClientScript.RegisterStartupScript(this.GetType(), "script", script);
        }

Set up a blank aspx page (in the example above, I called it ViewPDF.aspx) and in the page_load:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string filePath = Server.MapPath("~/Reports/") + Request.QueryString["fileName"];  //change path as required
                this.Response.ContentType = "application/pdf";
                this.Response.AppendHeader("Content-Disposition;", "attachment;filename=" + Request.QueryString["fileName"]);
                this.Response.WriteFile(filePath);
                this.Response.End();
            }
        }

This is just sample that takes a PDF filename via querystring and writes the file to display. You can play with the client script to change window properties as required.