Archive for September, 2016

Getting the current sequence value in Oracle

Who wants to know how to get the current sequence value in Oracle? Yes, I can hear the multitudes across the Internet saying “Me me me!”

SELECT last_number
FROM all_sequences
WHERE sequence_name = 'MY_SEQUENCE';
Advertisements

Leave a comment

jQuery fadeTo only firing once

I had a simple div that appears when a record saved. How simple?

    
        Record Saved    

The jQuery to make it popup then fade away after two seconds was not much more complex:

   function showMessage() {
       $("#rs").fadeTo(2000, 500).slideUp(500, function () { $("#rs").alert('close'); });
   }

This worked great… once. If I was updating multiple records, only the first time I saved a record would the popup appear. After that, nada.

My fix was to remove the div from the HTML code and modify the showMessage function. Instead of fading an existing div, I would create one on the fly:

   function showMessage() {
       $("body").append('
Record Saved
')     $("#rs").fadeTo(2000, 500).slideUp(500, function () { $("#rs").alert('close'); }); }

This worked. The problem with the original method is that the fadeTo method, on completion, set the div tag’s “display” property to “none”, meaning the div was completely removed from the page. So when a second record was updated, there was no div left to reference. The new method creates one every time the function is executes, and fades it away into oblivion.

,

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 })
        .ToArray();

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