Archive for category Error

Calling a .NET web service using jQuery

I was attempting to call a .NET web service using jQuery, but was getting this error message:

An attempt was made to call the method \u0027GetCoasters\u0027 using a GET request, which is not allowed

The key to fixing this was to attach the “ScriptMethod” attribute to the web service and setting the “UseHttpGet” property to “true”, which I show below:

public class OdometerService : System.Web.Services.WebService {

        [System.Web.Script.Services.ScriptMethod(UseHttpGet = true, 
              ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        public string GetCoasters() {
            return "Hello World";

And here is me calling the web service – try to contain your excitement:

 type: "GET",
 url: "http://localhost:12345/CoasterService.asmx/GetCoasters",
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 success: function (msg) {
 }, error: function (e) {
      alert('Bad stuff - ' + e.responseText);

Leave a comment

Showing MVC ModelState errors in the view

I wanted to show the various ModelState errors encountered in an MVC controller and display them in the view. So in my controller, I have a bit of code that loops through the errors and builds them in a string, adding an HTML line break between each error:

string errors = String.Empty;

foreach (ModelState modelState in ViewData.ModelState.Values) {
     foreach (ModelError error in modelState.Errors) {
          errors += “<br />” + error.ErrorMessage;

ViewBag.Message = “The following errors have occurred: “ + errors;

In the view, I show my ViewBag variable. Notice that I use the Html.Raw method to display the HTML code properly in using Razor:



Leave a comment

Object doesn’t support property or method ‘autocomplete’

I’ve seen this message before and it usually occurs for one of two reasons:

1) The jquery-ui.js file (which is required to get autocomplete working) wasn’t included in the page
2) The script file that contains your implementation of autocomplete occurs BEFORE the jquery-ui.js file

So if I have the following code in the script.js file to attach the autocomplete code to a textbox:

    source: function (request, response) {
        if (request.length < 4) {
        var customer = new Array();
            async: false,
            cache: false,
            type: "POST",
            url: "http://localhost:1234/MyController/Autocomplete",
            data: { "term": request.term },
            success: function (data) {
                for (var i = 0; i < data.length ; i++) {
                    customer[i] = { label: data[i].Value, Id: data[i].Key };
    select: function (event, ui) {

Your web page or view with the autocomplete functionality should look something like this. Note our script file is after the jquery-ui.js file:

@model UserVM

    ViewBag.Title = "Cool Page";


@using (Html.BeginForm("UserStuff", "MyController")) {
 @Html.EditorFor(model => model.User)

Leave a comment

Invalid ModelState with a bound DropDownList

I had a simple model, like so:

    public class AmusementPark {
        public List<SelectListItem> Coasters { get; set; }

I bound this simple model to a simple DropDownList, like so:

@Html.DropDownListFor(model => model.Coasters, Model.Coasters)

But when I submitted my model to the controller, I received an invalid ModelState. So I looked through the various errors in the ModelState with a bit of code:

    var errors = ModelState
        .Where(err => err.Value.Errors.Count > 0)
        .Select(err => new { err.Key, err.Value.Errors })

Lurking in the ModelState errors wasthis wonderful System.InvalidOperationException exception:

The parameter conversion from type ‘System.String’ to type ‘System.Web.Mvc.SelectListItem’ failed because no type converter can convert between these types.

Returning to the line in question, a DropDownList bound to a List of SelectListItem objects:

@Html.DropDownListFor(model => model.Coasters, Model.Coasters)

To resolve it, I first modified my model to have a key value:

    public class AmusementPark {
        public string CoasterId { get; set; }
        public List<SelectListItem> Coasters { get; set; }

Then I modified the view to set the new CoasterId as the first parameter in my DropDownListFor helper:

@Html.DropDownListFor(model => model.CoasterId, Model.Coaster)

Voila, the ModelState was now valid when posting.


Leave a comment

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=, 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

An exception of type ‘System.TypeInitializationException’ occurred

I was running code that called a static class, Repository, to populate an Application variable, like so:

Application["UserTypes"] = Repository.PopulateUserTypes();

When I did this, I received the following error:

 An exception of type 'System.TypeInitializationException' occurred in ScottApp.dll but was not handled in user code

The InnerException property was not terribly helpful:

Object reference not set to an instance of an object.

Turns out it was because I had forgotten to add a connection string, “ScottConn”, to the web.config. The static Repository class was attempting to reference this missing property when it loaded:

private static string connString = ConfigurationManager.ConnectionStrings["ScottConn"].ConnectionString;

Adding this missing string to the web.config file, the error disappeared.

Leave a comment

ORA-01008: not all variables bound

This was one of those “duh” error. I was attempting to retrieve data from Oracle using some simple ADO.NET code, like the code below, but I was receiving the error “ORA-01008: not all variables bound“.

string value = String.Empty;
using (OracleConnection conn = new OracleConnection(connString)) {
    using (OracleCommand comm = new OracleCommand("SELECT Description FROM ScottsTable WHERE ID = :id", conn)) {
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Add(":id", OracleDbType.Varchar2, 40).Value = "scott";
        value = comm.ExecuteScalar().ToString();

The problem was I was setting the OracleCommand’s CommandType property to StoredProcedure when it was actually straight text. Removing this line resolved the error.


Leave a comment