Archive for April, 2016

The Entity Framework provider type… exception when unit testing

While merrily going about my business creating an MVC project using Entity Framework 6 against an Oracle database, I decided that unit testing my data layer would be a fun, useful thing to do. So I added a reference to my MVC project into my unit test project and away I went. But not too far because I got this error:

{System.InvalidOperationException: The Entity Framework provider type ‘Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342’ registered in the application config file for the ADO.NET provider with invariant name ‘Oracle.ManagedDataAccess.Client’ could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application.

This was thankfully an easy issue to correct. In my unit test project, I added references to the Oracle.ManagedDataAccess and Oracle.ManagedDataAccess.EntityFramework DLLs, and I was testing like a madman.

Leave a comment

Hiding zero-value items in Google Charts

Working with Google Charts, I had three values I was putting into a bar chart. There were times when some of the values would be zero, and I didn’t want them to show. Usually, they didn’t, but once in a while, a sliver of red would peak through, as you can see in the image:

Google Charts

Google Charts

To get around this, I explicitly set the value in question to null instead of zero. So in my loop where I bind the data to the chart, I check to see if the value is zero. I’ve included the entire function for continuity. The variable “finalData” is a class passed from a Web API call, and the Foods is a generic List of class Food, which has four properties: Name, Ate, Eating, and WillEat:

function renderChart(finalData) {
var data = new google.visualization.DataTable();

data.addColumn(‘string’, ‘Text’);
data.addColumn(‘number’, ‘Ate’);
data.addColumn({ ‘type’: ‘string’, ‘role’: ‘tooltip’, ‘p’: { ‘html’: true } })
data.addColumn(‘number’, ‘Eating’);
data.addColumn({ ‘type’: ‘string’, ‘role’: ‘tooltip’, ‘p’: { ‘html’: true } })
data.addColumn(‘number’, ‘Will Eat’);
data.addColumn({ ‘type’: ‘string’, ‘role’: ‘tooltip’, ‘p’: { ‘html’: true } })
data.addColumn({ type: ‘string’, role: ‘annotation’ });

data.addRows(finalData.Foods.length);

for (var i = 0; i < finalData.Foods.length; i++) {

if (finalData.Foods[i] != null && finalData.Foods[i].Eating== 0) {
finalData.Foods[i].Eating= null;
}

data.setValue(i, 0, finalData.Foods[i].Name);
data.setValue(i, 1, finalData.Foods[i].Ate);
data.setValue(i, 2, ‘<p style=”font-size: 12; text-align: center; vertical-align: middle”>Ate</p>’);
data.setValue(i, 3, finalData.Foods[i].Eating);
data.setValue(i, 4, ‘<p style=”font-size: 12; text-align: center; vertical-align: middle”>Eating</p>’);
data.setValue(i, 5, finalData.Foods[i].WillEat);
data.setValue(i, 6, ‘<p style=”font-size: 12; text-align: center; vertical-align: middle”>Will Eat</p>’);
data.setValue(i, 7, ”);
}

var options = {
title: ‘Favorite Foods’,
titleTextStyle: { ‘fontSize’: 22 },
fontSize: 12,
width: 570,
height: 275,
legend: { position: ‘none’ },
isStacked: true,
tooltip: { isHtml: true, textStyle: { fontName: ‘Arial’, fontSize: 16 } },
series: {
0: { color: ‘green’ },
1: { color: ‘red’ },
2: { color: ‘blue’ },
}
};

var chart = new google.visualization.BarChart(document.getElementById(‘chartDiv’));
chart.draw(data, options);
}

Leave a comment

405 Error – The requested resource does not support http method ‘PUT’

While using a PUT request against a Web API call to insert a record into the database, I ran into the 405 error, “The requested resource does not support http method ‘PUT'”. I found a useful link on Stack Overflow that led me to the solution. Here is what worked for me, but I’ll first start by showing the basic call that didn’t work:

    public class ScottController : ApiController {
        public void Put([FromBody]DTO item) {
           ScottRepository.CreateItem(item);
        }
    }

The DTO class holds the fields of the object that I am trying to insert:

        public class DTO {
            public string ID { get; set; }
            public string Name { get; set; }
            public DTO() { }
        }

Those alone were not working. To get the PUT request running, I modified the controller class, adding a RoutePrefix attribute to reference the name that my jQuery call would be using, then added a Route attribute on the PUT request. Here is my final result:

    [RoutePrefix("api/Scott")]
    public class ScottController : ApiController {
        [Route]
        public void Put([FromBody]DTO item) {
           ScottRepository.CreateItem(item);
        }
    }

In case you are curious, here is my jQuery code to call Web API:

      var item = { ID: 'ABC123', Name: 'Bill'};
      $.ajax({
            url: 'http://www.bla.com/api/Scott',
            type: 'PUT',
            dataType: 'json',
            cache: false,
            contentType: "application/json",
            data: JSON.stringify(item),
            success: function (d) {
                // woohoo!                },
            error: function (jqXHR, textStatus, errorThrown) {
                alert('sadness');
            }
      });

Leave a comment

Why a Cordova function executes at page load

I have recently been experimenting with the Cordova libraries to create cross-platform mobile applications using Visual Studio 2015. If you are interested in learning about Cordova, I found this tutorial to be very helpful.

Being new, I just wanted to go off-script and create a button to say “Hi”. So I had a basic HTML page with a reference to my JavaScript file (I have ripped out all the other code to make the example more readable):

<!DOCTYPE html>
<html>
<head>
    http://scripts/find.js
</head>
<body>
    <button id="find" data-role="button">Find</button>
</body>
</html>

In my JavaScript file, find.js, I had a simple method to display the results of a JS alert:

$(document).ready(function () {
    $('#find').bind("click", alert('Hi'));
});

My expectation was that this would only execute when I click the button, which my years of development experience led me to believe. But things were different here, and I was getting the “Hi” message when my application loads.

Luckily, I found a nice Stack Overflow post explaining the problem, and the solution. To fix this behavior, I had to stick the alert call into a method and modify the bind statement:

$(document).ready(function () {
    $('#find').bind("click", alerty);
});
function alerty() {
    alert('Hi');
}

Notice that when I reference “alerty” in the bind statement, I don’t include any parentheses.

Leave a comment

Why is my model attribute null?

While I can’t explain every reason, here is one reason I found. First, my rather simple model:

    public class User {
        public string ID { get; set; }
        public string Email { get; set; }
        public Guid SecretId{ get; set; }
    }

I was manually populating the model from the database and passing it off to the view from the Home controller like so:

     public ActionResult Index(string id) {
         User user = new User();
         user.ID = id;
         // Various other stuff...
         user.SecretId = Guid.NewGuid();
         return View("Index", user);
     }

The user can do some simple modifications in the view, but it’s not terribly complex. Then perform a POST:

   @model ScottProj.Models.User
   
    @using (Html.BeginForm("Process", "Home", FormMethod.Post)) {         @Html.LabelFor(m => m.ID)         @Html.LabelFor(m => m.Email )         @Html.TextBoxFor(m => m.Email )             }

When it got to the Process controller action, the “SecretId” was missing, most likely because there wasn’t anyplace in the view that there field was referenced, no form field that says “This is the SecretId value”. I didn’t want to show that value on the screen, so instead, I added a hidden field to hold that value:

   @Html.HiddenFor(m => m.SecretId)

And that did. My view now had a placeholder for the value so it could be sent back to the controller in the POST request.

,

Leave a comment

Matching a password in the Oracle database

I was writing a utility to unlock a user account when the user knew their password. The unlocking part was easy (assuming the ID your are connecting as has permissions to unlock an account):

ALTER USER Scott ACCOUNT UNLOCK;

My problem was trying to match the password the user entered with the one that is stored in the database – I only wanted to unlock a password if the user was really the user (i.e. knew their user ID and knew their password). But I found a fantastic script at http://www.petefinnigan.com/testpwd.sql that did just what I needed.

Once I ran the script to load the function, I just needed to call the function like so:

 select testpwd('Scott', 'bla') from dual;

This will return “Y” for the right password and “N” for an incorrect password. Super slick.

Leave a comment