scottstoecker

I've been a developer since the mid-90's, working primarily with Microsoft technologies such as ASP, .NET, and SQL Server. I live and work in and around Lansing, MI.

Homepage: https://scottstoecker.wordpress.com

Templates can be used only with field access… error using ToShortDateString()

While binding a partial view to a model, I noticed the my nullable DateTime field was showing the time portion. Adding attributes to the model didn’t work, so I attempted to use the ToShortDateString() method:

@Html.TextBoxFor(model => Model.StartDate.Value.ToShortDateString(), new { @class = “form-control”, @maxlength = “10” })

However, this caused the error “Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions”.

To get around this, set the shortened date value to a string, and used that as the binding to the textbox:

var startDate = Model.StartDate.Value.ToShortDateString();

@Html.TextBoxFor(model => startDate, new { @class = “form-control”, @maxlength = “10” })

Advertisements

Leave a comment

Unable to find the requested .Net Framework Data Provider. It may not be installed.

While moving a web application to a new web server, an application which used Entity Framework returned this message when loading:

ArgumentException: Unable to find the requested .Net Framework Data Provider. It may not be installed.

   System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName) +959971

   System.Data.EntityClient.EntityConnection.GetFactory(String providerString) +26

 

And this message also appeared a bit lower on the yellow screen of death:

ArgumentException: The specified store provider cannot be found in the configuration, or is not valid.

 

The problem was with the DbProviderFactories elements in the machine.config file – it didn’t have the correct drivers installed. To fix it, I opened Notepad (Visual Studio wasn’t installed on the server) as an administrator because the file wouldn’t let me save it otherwise. I scrolled down the <system.data> section, which initially looked like this:

<system.data>

<DbProviderFactories />

</system.data>

 

I replaced it with the section below which I pulled from my local machine. I have left in all the libraries in case that helps, though in our environment, we didn’t use the SQL Server CE drivers:

<system.data>

             <DbProviderFactories>

<add name=”ODP.NET, Managed Driver” invariant=”Oracle.ManagedDataAccess.Client” description=”Oracle Data Provider for .NET, Managed Driver” type=”Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342″/>

<add name=”ODP.NET, Unmanaged Driver” invariant=”Oracle.DataAccess.Client” description=”Oracle Data Provider for .NET, Unmanaged Driver” type=”Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342″/>

<add name=”Microsoft SQL Server Compact Data Provider 4.0″ invariant=”System.Data.SqlServerCe.4.0″ description=”.NET Framework Data Provider for Microsoft SQL Server Compact” type=”System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91″/>

<add name=”Microsoft SQL Server Compact Data Provider” invariant=”System.Data.SqlServerCe.3.5″ description=”.NET Framework Data Provider for Microsoft SQL Server Compact” type=”System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=3.5.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91″/>

               </DbProviderFactories>

</system.data>

Leave a comment

ORA-00918: column ambiguously defined with CASE column

I had an Oracle query that was returning the error “ORA-00918: column ambiguously defined” when I tried to run it:

SELECT * FROM ( 
  SELECT ID, CASE
    WHEN something THEN something else
    ELSE yet more
  END CASE
FROM ScottsTable
UNION ALL
  SELECT ID, CASE 
    WHEN something THEN something else
    ELSE yet more
  END CASE
FROM ScottsTableHistory
) ORDER BY 1 ASC, 2;

The problem was originating in the first query’s CASE statement. I needed to put an alias on the first CASE statement to avoid the naming duplication that was causing Oracle to hate me. Here is the final, modified query:

SELECT * FROM ( 
  SELECT ID, CASE
    WHEN something THEN something else
    ELSE yet more
  END "MY COLUMN"
FROM ScottsTable
UNION ALL
    SELECT ID, CASE 
    WHEN something THEN something else
  ELSE yet more
END CASE
FROM ScottsTableHistory
) ORDER BY 1 ASC, 2;

Leave a comment

Dude, why does my site look janky on the test server?

If I were a skater boy developer, that is what I would have said to myself when I saw how awful my site looked after putting it on a new development server. Instead, I cursed and wept and gnashed my teeth, because that seemed for in character for me. After all, the relative links were all correct, and the CSS files were where they should be. And yet, disaster when I viewed it on IE installed on the web server itself.

Looking at the F12 Developer Tools, I switched to the “Console” tab and found an interesting line about compatibility view:

Compat1

Luckily, the site Sonic Wall had what I needed. The problem was that IE 11 was set to view intranet sites in compatibility mode. To get around this for IE 11 (see the Sonic Wall site, which has helpful instructions for several versions of IE), go to Tools, Compatibility View Settings, and uncheck the “Display intranet sites in Compatibility View” checkbox:

Compat2

Site was then janky-less.

Leave a comment

HTTP Error 403.14 for MVC site

I was setting up a new MVC 5 site on a Windows Server 2016 Datacenter machine when I ran into this error:

HTTP Error 403.14 – Forbidden – The Web server is configured to not list the contents of this directory.

I didn’t actually want the web server to list the contents, so that was not the problems.

This fancy Internet thing had lots of suggestions, the best ones at Stack Overflow. However, those didn’t solve my problem. What did solve my problem was to correct a dumb mistake. Here was my IIS configuration:

IIS

 

 

 

You’ll notice the ever-populate “ScottSite” is looking like a sad, plain folder. And that was my problem – I hadn’t turned it into an application. Right-clicking on my non-application and selecting “Convert to application” fixed my problem. Magic.

IIS

 

 

 

Leave a comment

Type or namespace name could not be found when building in Release mode

When building a web application with Visual Studio in Debug mode, I wasn’t getting any errors. But when I switched to Release mode, I received this error:

The type or namespace name ‘ScottStuff’ could not be found (are you missing a using directive or an assembly reference?)

There was no conditional logic to exclude that ScottStuff library based on the configuration I was using.

The problem was that when I made the reference to the ScottStuff DLL, I had included it from a different directory, c:\SomeDirectory\ScottStuff.dll. To fix the issue, I instead referenced the DLL from the bin folder of the project I was working on, c:\Projects\ScottProject\bin\ScottStuff.dll. After doing this, the project compiled successfully.

Leave a comment

401 – Unauthorized in an ASP.NET project

While debugging a new to me (I didn’t write it) ASP.NET project locally, I came across this error when the default.aspx page loaded:

401 - Unauthorized: Access is denied due to invalid credentials.
You do not have permission to view this directory or page using the credentials that you supplied.

There are several reasons why this could happen, but in my case, it was because the application was set to reject unauthenticated users. In the web.config file, there was this section:

    <authorization>
      <deny users="?" />
    </authorization>

Disabling the <deny> tag resolved the issue.

Leave a comment