Diamonds and Alcohol

June 17, 2014

“Your stats are booming!”, sayeth Teh WordPress.

But really, 160 spam comments about (mostly) diamonds and alcoholism within 24 hours is not what I expected.

Stumbling Upon the “Not Pre-Compiled” Error Message

May 12, 2014

I maintain an ASP.Net application, and I recently had to add a couple of new features. Development started with .Net 1.1, then 2.0, then 4.0. Since we were already on .Net 4, the new features are implemented in MVC3.

Everything worked fine until I wanted to deploy the ASP.Net-plus-MVC application.

I hit Publish, zipped the result, and unzip everything into its usual directory.


The file ‘/somepage.aspx’ has not been pre-compiled, and cannot be requested.


Die Datei /somepage.aspx wurde nicht vorkompiliert und kann nicht angefordert werden.

I asked the internetz, and they suggested checking referenced assemblies. So I installed the MVC3 setup (on IIS8), just in case. But that did not resolve my problem.

While browsing thru tons of useless (and sometimes wrong) tips and tricks, I found this answer on SO:

I got this error when I upgraded a site from 2.0 to 4.0. The error was caused by a file PrecompiledApp.config in the site’s root directory. Once I deleted that file, the site started working.

Then it struck me: I had deployed the previous version of the web application using my build process which also compiles (and merges) .aspx files.

I have not yet found the time to adapt this build process to also support MVC projects (or test it whether it already does!), so I deployed what Publish produces. And this is not pre-compiled pages.

So when I overwrote the old version with the new version, the file PrecompiledApp.config remained.

After deleting the file, the web application started up again.

Calling a User Defined TSQL Function using NHibernate Linq

April 26, 2014

NHibernate Linq supports calling UDFs, but the implementation is not straight-forward.

I previously wrote about adding support for .Net string comparison functions in NHibernate Linq. This strategy uses existing .Net methods (such as methods of the System.String class) and maps them onto Linq tree operations which in turn are translated into SQL by the NHibernate database driver.

This time, we want to access a user defined TSQL function which cannot be interpreted as a “method” of a “data type” because it contains generic parameters, such as integers or date values, and is not specific to a single table, such as this example on SO.

Nevertheless my solution is based on that example.

The function I want to access is called dbo.Display_Record and is used to decide whether a record can be displayed in the UI based on a given date range.

CREATE FUNCTION [dbo].[Display_Record]
  (@ValidFrom DATETIME, @ValidUntil DATETIME)
  RETURN 0 (false) or 1 (true)

Note that the return type of the function should not be boolean, due to the interoperation of NH and SQL Server (see bug report here) – use integer values instead.

First, we need a declaration and dummy implementation in C#

    public static class dbo
        public static int DisplayRecord(DateTime? from, DateTime? until)
            return 0;

Next, an Hql (the SQL-like language NH uses internally) generator registry must be registered for the method call “dbo.DisplayRecord”:

    public class DisplayRecordLinqToHqlGeneratorsRegistry
        : DefaultLinqToHqlGeneratorsRegistry
        public DisplayRecordLinqToHqlGeneratorsRegistry()
            : base()
                () => dbo.DisplayRecord(null, null)), 
                new DisplayRecordMethodsHqlGenerator());

And an Hql generator

    public class DisplayRecordMethodsHqlGenerator
        : BaseHqlGeneratorForMethod
        public DisplayRecordMethodsHqlGenerator()
            SupportedMethods = new[] {
                  () => dbo.DisplayRecord(null, null))

        public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, 
            ReadOnlyCollection<Expression> arguments, 
            HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
            return treeBuilder.MethodCall("dbo.Display_Record",

arguments[0] and arguments[1] are the arguments passed to our .Net function, and need to be added to the Linq tree.

Finally, our generator registry needs to be registered using Mapping By Code configuration:


Now we can write NH Linq queries such as

var records = session.Query<MyTable>
    .Where(record => dbo.DisplayRecord(record.ValidFrom, record.ValidUntil);

and the WHERE condition correctly references our UDF function.

Querying Facebook Like Count and Public Page Properties

April 18, 2014

There are various way to query the Like count of Facebook pages.

If you just want to want the live stats of a given Facebook page, Quintly provides a live statistics page. Upon entering the FB page name ([page-name][page-name]), the page displays a live chart updating every couple of seconds.

But you can also query Facebook directly using the FQL API or the Graph API.

Here’s a sample URL for the FQL API

(enter this into the navigation bar. [my-fb-url] is the complete URL starting with “https://

The result is an XML document in the form

<fql_query_response list="true">

and like_count or total_count will be the values you are looking for.

The Graph has a simpler approach, using simply

Here the result is given as JSON value, and the relevant JSON property is “likes”:

   "id": "74865038590",
..."likes": 194804,

If you want to query repeatedly, here’s a simple PowerShell solution using FQL:

$wc = New-Object system.Net.WebClient;
$response = $wc.downloadString("
$xml = [xml] $response


jQuery Autocomplete Inconsistencies

April 17, 2014

I guess I found an inconsistency in jQuery’s autocomplete() widget, which does not cause a selected value to be reset when clearing the input field.

Steps to reproduce (checked with 1.8.3 and 1.11.0):

  • Type the beginning of a string that is provided by the autocomplete source
  • Tab away from the input
  • “autocompletechange” fires, but the value is not valid, so the input field value is reset
  • Type the beginning of string (as above)
  • Select an entry using cursor keys
  • “select” fires (and sets the corresponding key value for the selected text value in a callback function)
  • Double-click the input to select the full text of the input field
  • Delete selected text (backspace or right-click Cut)
  • Neither “autocompletechange” nor “select” fires
  • Tab away from input field
  • Neither “autocompletechange” nor “select” fires

From the console.log:

blur ” 5:81
mouseup ” 5:90
keyup ‘r’ 5:87
keyup ‘re’ 5:87
keyup ‘rei’ 5:87
keyup ‘re’ 5:87
change ‘re’ 5:84
autocompletechange null 5:78
Nothing selected, input was re common.js:114
blur ” 5:81
mouseup ” 5:90
keyup ‘re’ 5:87
keyup ‘re’ 5:87
keyup ‘rei’ 5:87
keyup ’6749′ [this is the key value!] 5:87
Selected: [some text] (6749) common.js:114
keyup ‘[some text] 5:87
keyup ‘[some text] 5:87
keyup ‘[some text] 5:87
keyup ” 5:87
change ” 5:84
blur ”

The value displayed in single quotes is the .value property of the input field, “rei” being the string that corresponds to the beginning of a valid value in the source.

The only workaround that comes to mind is to use the keyup and mouseup events to check whether the input’s value property is empty, and then reset the key field accordingly.

A jQuery Alternative for ACT CalendarExtender

April 6, 2014

As I extend an ASP.Net application with MVC functionality, I also need to replace some of the components that the AjaxControlToolkit provides. Case in point, the CalendarExtender.

The CE is declared in markup like this:

<ajax:CalendarExtender id=”ce” runat=”server” TargetControlID=”edDate” Format=”dd.MM.yyyy”>

edDate being the ASP.Net TextBox control containing the selected date.

The simplest replacement is, of course, the Datepicker widget found in the jQuery UI library.

Integration the Datepicker is a bit more effort as compared to the other controls in this series, but not much.

First we need to reference the required .js and .css files of jQuery and jQuery UI:

<script type="text/javascript" src="@Url.Content("~/script/jquery-1.8.3.js")"></script>
<script type="text/javascript" src="@Url.Content("~/script/jquery-ui-1.9.2.custom.min.js")"></script>
<link href="@Url.Content("~/css/jquery-ui-1.9.2.custom.css")" rel="stylesheet" type="text/css" />

Depending on the options you used for the CalendarExtender, the initialization for the Datepicker may look like this:

  changeMonth: true,
  changeYear: true,
  showButtonPanel: true
$("#edDate").datepicker("option", "dateFormat", "");
$("#edDate").datepicker("option", $.datepicker.regional["de"]);

For more options, see the sample page and press View Source, or browse the documentation.

Note that while the CE requires “yyyy” for 4-digit years, the Datepicker interprets “yy” as 4-digit years, and will insert the year twice if you use “yyyy”.

I’m not sure whether the localization files are part of the default jQuery UI downloads – if not, you can find the localized strings for Datepicker in the GitHub repository.

Lastly, the CE provides a “Today” button which sets the current date and closes the calendar, whereas Datepicker just navigates to the current date in the calendar control, but does not select it.

To change the Datepicker behavior, I use the code found in an answers on SO:

$.datepicker._gotoToday = function (id) {
    var target = jQuery(id);
    var inst = this._getInst(target[0]);
    if (this._get(inst, 'gotoCurrent') && inst.currentDay) {
        inst.selectedDay = inst.currentDay;
        inst.drawMonth = inst.selectedMonth = inst.currentMonth;
        inst.drawYear = inst.selectedYear = inst.currentYear;
    else {
        var date = new Date();
        inst.selectedDay = date.getDate();
        inst.drawMonth = inst.selectedMonth = date.getMonth();
        inst.drawYear = inst.selectedYear = date.getFullYear();
        this._setDateDatepicker(target, date);
        this._selectDate(id, this._getDateDatepicker(target));

See also here for further discussion.

Finally, if the jQuery UI skins do not fit in the design of your application, open the developer tools of your browser (F12) and find out which jQuery UI CSS classes define the look of the Datepicker control. In my case, I added a bit of CSS to adjust the control:

.ui-datepicker table.ui-datepicker-calendar {
    font-size: 11px;    
.ui-datepicker table.ui-datepicker-calendar td
    width: 17px;
.ui-datepicker table.ui-datepicker-calendar td a 
    padding: 0px;
.ui-datepicker .ui-datepicker-title select {
    font-size: 0.9em;
    margin: 1px 0;
.ui-widget button {
    font-size: 0.8em;

Note that the overriding style definition selectors must be more specific than the original jQuery UI declarations.

Aaaand we are done! ;)

Batch Unblock Files

April 2, 2014

If you download a file, or receive a file by email, and save it to disk, the file is marked as downloaded from an external computer, and cannot be executed or, depending on the application, opened. You need to open the file properties dialog, and click “Unblock” to resolve the situation.

The same is true for zip files and other archives: When extracting the archive, the “blocked” flag may be set on every extracted file, and the “Unblock” button is not available if you select more than one file and open the properties dialog.

Fortunately, there is an easy solution that I found in a comment on SO:

for /F %a in ('dir /r/b/s') do @echo .>%a:Zone.Identifier:$DATA



Get every new post delivered to your Inbox.

Join 66 other followers