Archive

Author Archive

HowTo: Use latest C# features in MVC5 Razor views (.cshtml)

Having recently updated an ASP.net MVC web app from MVC4 to MVC5 and from .NET 4.5 to .NET 4.7.2 I was expecting Razor views (.cshtml files) to use the latest C# compiler, especially since at Properties/Build/Advanced option for the web project one read “C# latest major version (default)”.

However that was not the case and trying to use newer C# language features like the ?. ternary conditional operator or interpolated strings (or nameof etc.) would show errors like

Feature ‘interpolated strings’ is not available in C# 5. Please use language version 6 or greater.

Luckily there is a workaround for using the latest C# compiler in MVC5. Just need to add the NuGet package https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/ to one’s project as explained at https://dusted.codes/using-csharp-6-features-in-aspdotnet-mvc-5-razor-views. Alternatively one could move their project to ASP.net Core, which is a more drastic move though.

After doing it I started seeing Intellisense issues in .cshtml like:

The type ‘Expression<>’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Core …

Tried to add the System.Core assembly to the project, but wasn’t allowed (it said the Build system was adding it). Adding System.Core as a NuGet package would mean moving to .NET Core which I wasn’t ready to try with that project yet.

Seems there was an easy solution to that, just closed and reopened the Visual Studio solution and did a Rebuild and all was fine after that.

Categories: Posts Tags: , , , , , ,

Fix: Word print view showing no margins and pages appear collated

I’m often asked by friends and family to assist them on frustrating technical issues they can’t resolve by themselves. Thanks to remote access / support software like TeamViewer, this is a piece of cake nowadays.

The solution to such problems isn’t always straightforward though. Just came across a Microsoft Word installation where suddenly the Print view had started using a strange layout with grid lines in the background, no margins and with the consecutive pages showing collated as you scrolled in the document. This defeats the whole WYSIWYG (What-You-See-Is-What-You-Get) concept that most people like about modern word processors.

Removing the grid lines is straightforward from the View menu (Appearance section), but to fix the other issues, the only way I found was, at the Zoom section of the View menu, to change to Multiple Pages, then change to Single Page, then Set Zoom to 100% and finally Set zoom to Page Width. After all is fixed you can then switch those settings to how you prefer them (say zoom 100% instead of page width).

If that doesn’t work, you could try resetting Microsoft Word user settings to their defaults, both an automated and a manual way is offered at:

https://support.microsoft.com/en-us/help/822005/how-to-reset-user-options-and-registry-settings-in-word

Categories: Posts Tags: , , , , , ,

Fix: “The Global element ‘xx’ has already been declared” warnings in config files after .NET framework upgrade in Visual Studio solution

Had just converted to target .NET framework 4.7.2 a Visual Studio solution full of 4.5 libraries, console apps and an MVC 5.0 (recently converted from 4.0) web app and all seemed to build fine, but then noticed that with web.config of the MVC web app open in the editor, it was showing lots of warnings of the form:

The Global element ‘xx‘ has already been declared in …

and marking them in the text with blue underscores under some whitespace

Seems others have had this issue too with configuration files (both ASP.net and Entity Framework ones) after changing target .NET framework:

https://stackoverflow.com/questions/11816916/fix-the-global-element-configuration-has-already-been-declared

Luckily the solution was hidden in that thread, just had to pick the best one (read on below)

– at https://stackoverflow.com/a/52274659/903783 one reads (see related screenshot there):

I noticed this issue with my VS2017.

Changing DotNetConfig.xsd to use "Automatic" solved the problem.

This doesn’t seem to work permanently (only worked once for a while for me)

– at https://stackoverflow.com/a/28114738/903783 is says:

With the symptoms as described in the question, and using Visual Studio 2013 (Update 4), I could see in the XML Schemas [sic] dialog that both DotNetConfig.xsd and DotNetConfig40.xsd were selected for use.

I’m using a .NET Framework 4.0 project.

The two XSD files conflict with each other, each defining the same elements, causing the warnings to be emitted.

These schema files are contained in the %programfiles(x86)%\Microsoft Visual Studio 12.0\xml\Schemas\ folder.

DotNetConfig.xsd is in the 1033 sub-folder and appears to be the newer, more complete version.

No matter what settings I selected in XML Schemas, I could not deselect, or remove DotNetConfig40 nor DotNetConfig. I tried "Remove", and changing the Use parameter from "Use this schema" to "Automatic" and then "Do not use this schema".

No matter what was selected, for either file, when I would return to the dialog, both would be selected for use. I also tried the trick of moving to another row before pressing "OK" to no avail.

Finally, I renamed the DotNetConfig40.xsd file to DotNetConfig40 DO NOT USE.xsd to prevent it from being loaded. This immediately solved the problem.

This does work (adapting the path for VS2017), but one isn’t sure (unless they check the file dates maybe) which file to keep, in my case it was 1033/DotNetConfig.xsd and DotNetConfig45.xsd, nor are they sure this won’t cause any side-effects in other solutions/projects.

– Finally, at https://stackoverflow.com/a/33823331/903783 it read:

I struggled with this for a while as well. Turns out, my version of the problem originated from the hidden {PROJECTNAME}.SUO file created by Visual Studio.

My guess is, VS will cache the XSD schema associations in this file. The warnings popped up after I changed the target framework, and they disappeared after I deleted the SUO file and restarted VS.

This fixed it permanently. Deletion of the .suo ended up with the DotNetConfig.xsd and removed the DotNetConfig45.xsd from the XML schemas list for the projects in the solution. In fact I deleted just a .suo file with no filename part, just a file extension, found by typing “.suo” at the search filter in the top-right of the solution’s folder window in Windows 10 File Explorer.

How-to: get int value via ADO.net SqlDataReader using column name

Based on Sam Holder’s answer at https://stackoverflow.com/questions/7388475/reading-int-values-from-sqldatareader/54296026, just contributed an extension method for fetching Int32 values via ADO.net’s SqlDataReader, without jumping through hoops (aka first fetch column ordinal [number] by name, then fetching the int value passing the column ordinal).

Would be nice if Microsoft was providing such things out of the box.

namespace adonet.extensions
{
  public static class AdonetExt
  {
    public static int GetInt32(this SqlDataReader reader, string columnName)
    {
      return reader.GetInt32(reader.GetOrdinal(columnName));
    }
  }
}

and use it like this

using adonet.extensions;

//…

int farmsize = reader.GetInt32("farmsize");

assuming there is no GetInt32(string) already in SqlDataReader – if there is any, just use some other method name instead

Microsoft AI solutions

* AI business solutions: https://partner.microsoft.com/en-US/solutions/practice-areas/artificial-intelligence

* AI platform: https://azure.microsoft.com/en-us/overview/ai-platform/

at the section «Open and comprehensive platform» (scroll down) and see the three tabs AI tools/AI frameworks/AI related infrastructure

* Azure (cloud) Machine Learning: https://azure.microsoft.com/en-us/overview/machine-learning/

– Machine Learning service (TensorFlow, PyTorch, or Jupyter etc.)
https://azure.microsoft.com/en-us/services/machine-learning-service/
https://docs.microsoft.com/en-us/azure/machine-learning/service/
– Machine Learning Studio (visual drag-drop/flow based)
https://azure.microsoft.com/en-us/services/machine-learning-studio/
https://docs.microsoft.com/en-us/azure/machine-learning/studio/
– Data Science VM (collection of third-party Machine Learning tools)
https://docs.microsoft.com/en-us/azure/machine-learning/data-science-virtual-machine/

* Azure DataBricks (Apache Spark-based BigData analytics): https://azure.microsoft.com/en-us/services/databricks/

* Visual Studio AI extensions:

https://docs.microsoft.com/en-us/azure/machine-learning/data-science-virtual-machine/

* Visual Studio Code (cross-platform IDE) AI extensions:

https://visualstudio.microsoft.com/downloads/ai-tools-vscode

* Bots (Conversational):

https://dev.botframework.com/

https://blogs.technet.microsoft.com/uktechnet/2018/02/02/an-introduction-to-the-microsoft-bot-framework-2/

https://app.pluralsight.com/library/courses/microsoft-bot-framework-getting-started-update/table-of-contents

* Cognitive Services: (pre-trained models for Speech, Face recognition, generating Q&A from unstructured text etc.)

https://azure.microsoft.com/en-us/services/cognitive-services/

https://app.pluralsight.com/library/courses/microsoft-azure-developer-creating-integrating-ai-azure-services/table-of-contents

* AI blog: https://azure.microsoft.com/en-us/blog/topics/artificial-intelligence/

* more resources for developers:

https://partner.microsoft.com/en-us/isv-resource-hub/develop-or-migrate-your-app

Fix: jqGrid search operator menu items jumping around on hover

A colleague noticed earlier on today, that on the Chrome browser they were using, the search operator menu items of a jqGrid on a web app’s UI were jumping around on hover, making it practically impossible to select one.

image   image

Seems this had started occurring on that ASP.net MVC web application, after a recent update to the latest jQuery-UI NuGet package.

Probably the issue wouldn’t occur if it wasn’t using the older free version of jqGrid. Will probably transition it eventually to free-jqGrid, the free fork (and quite evolved since forked) of the older free version of jqGrid, since jqGrid’s newer version is now commercial.

For the time being, I ended up adding this fix to Content/Site.css of the ASP.net MVC webapp:

/* jqGrid search operator menu item hover (uses jQuey-UI) */

a.ui-corner-all.g-menu-item.ui-state-hover {
    border: 1px #cccccc !important; /* fix for Chrome: removed "solid" (was causing hovered items to resize and menu to relayout, making it too hard to select them */
    /*border: 1px solid #cccccc;*/ /* this is used by jQuery-UI for .ui-state-hover */

    color: blue; /* showing blue color for hovered over menu text, should show better than a hover border since selected item already has a border itself */
}

This also has the added extra that on hover it shows the hovered item’s text in blue:

image

%d bloggers like this: