Author Archives: matt

Package libicu60 is not available => solved

I keep getting this when trying to get dotnetcore running on linux:


Package libicu60 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package ‘libicu60’ has no installation candidate

To solve this, I’ve done the following:

Install libicu60.

Then get the the runtime, and finally install the SDK.

ASP.NET WebForms – Use ICallbackEventHandler to lighten the load of PostBacks

Much has been written over the years regarding the drawbacks of ASP.NET and how MVC/SPAs solve many of these problems, so there’s not much to rehash there. However, if you’re stuck working with older technology, there is a handy tool to make your ASP.NET WebForms site more responsive. Implementing the ICallbackEventHandler interface in your WebForms will help do the following:

  1. Limit the amount of data that must be retrieved from the server
  2. Maintain user’s experience by eliminating the need for entire page re-rendering.

To illustrate, lets create a callback that calculates a person’s age.

  1. Create a new blank ASP.NET Web Application
  2. Add a Web Form and call it default.aspx
  3. Add “ICallbackEventHandler” to default page’s code-behind class file.
  4. There are two methods created. GetCallBackResult and RaiseCallbackEvent.
  5. Register a client script block in the Page_Load method. This script block represents the client side JavaScript that acts as the glue between the client and server.
  6. Press “Play”. When default.aspx loads in the browser, view the source html. Depending on the browser, you’ll see something similar to this:

    Notice that the Page.ClientScript.GetCallbackEventReference in our code renders a function call to “WebForm_DoCallback”. We did not create this function, .NET did, and you can see what it looks like by clicking on the script link beginning with “/WebResource.axd”. I won’t pasted the entire contents of that file, but the method signature looks like this:

    This function’s arguments align step 5’s server side Page.ClientScript.GetCallbackEventReference method:

      • eventTarget – The server Control that handles the client callback. The control must implement the ICallbackEventHandler interface and provide a RaiseCallbackEvent method.
      • eventArgument – An argument passed from the client script to the server RaiseCallbackEvent method.
      • eventCallback – The name of the client event handler that receives the result of the successful server event.
      • context – The client script that is evaluated on the client prior to initiating the callback. The result of the script is passed back to the client event handler.
      • errorCallback – The name of the client event handler that receives the result when an error occurs in the server event handler.
      • useAsync – true to perform the callback asynchronously; false to perform the callback synchronously.

     

  7. Here’s what the code behind looks like now:


    The two important methods are GetCallbackResult and RaiseCallbackEvent. RaiseCallbackEvent is where the client’s payload enters our code. Immediately after executing this, the GetCallbackResult function returns a string, back to the client.

    The *.aspx page looks like this:

  8. You’ll notice the there are two JavaScript function definitions and a single invocation. onCalcualteAgeSuccess and onCalculateAgeError were registered as the callback event handlers in step 5. They will wait patiently for the server to return something. The Javascript function that is being invoked is the CalculateAge function that we also defined in step 5. It is essentially a wrapper function for the native ASP.NET WebForm_DoCallback function.
  9. Press play and try both out. On my system, the callback returns 300 KB and the full PostBack returns a whopping 1700 KB, all to retrieve a few characters of data!

CAUTION: This SQL will DELETE all rows in your table

I came across this gem the other day. Fortunately, I ran it locally first.

There are two tables. Person and AlienPerson. Lets create the tables and seed with some data.

Try to run a bad select statement:

Here’s the baddie. This will delete all data in the Person table:

To get this to happen, the following must be true:

1. The SELECT field in the sub-query must be named the same as the field in the outer WHERE clause.
2. The inner WHERE clause must reference a field in the outer table. Otherwise, the DELETE will not run.

Using:

Microsoft SQL Server 2012 – 11.0.5343.0 (X64)
May 4 2015 19:11:32
Copyright (c) Microsoft Corporation
Developer Edition (64-bit) on Windows NT 6.3 <X64> (Build 10586: )

Nullable booleans reconsidered

If you’re about to use a Nullable<bool>, I would consider using an Enum or other data structure instead. It may help you avoid mistakes when the meaning of NULL is not known.

Recently, I mishandled a Nullable<bool>. I won’t go into the details of the mistake, but lets just say it was bad enough to require a full rollback. I began to wonder how practical it is to use in most applications. (In part in an attempt to hold on to my broken pride as a result of the stupid mistake 🙂 )

In this stackoverflow post, the answer by “Lee” provides a good example of when to use a Nullable<bool> type:

Something can be true, false or undefined for many reasons. How would one answer “Is your third child a girl?” if one only has two children? Both true and false are incorrect. Null would be appropriate as saying that the comparison doesn’t apply.”

This seems to make sense, but the more time I spend with it, the answer becomes FALSE in practice instead of NULL. NULL is just too fuzzy.

Before answering this question one would have to assign a very specific meaning to NULL. Does NULL mean there was an error in the calculation? What if there is an error in the calculation before it is determined that there are only two children? (I would also argue that if there are indeed only two children, this could be handled before the question was posed to the program.)

Because we do not know what NULL means and because it is most definitely not TRUE (because, how could it be?)  the BEST answer is FALSE.

Also, if this question indeed returns NULL, we’ve introduced a new definition to the domain. What is the scope of this definition?

So it would appear that TRUE or FALSE states represent a certainty regarding an outcome. NULL can be conceived of in at least three different states.  For example, the statement “Thomas went to the bar last night.”

  1. TRUE – Yes, Thomas went to the bar. (likeliest answer if you knew Thomas)
  2. FALSE – No, Thomas did not go to the bar.
  3. NULL – Huh?
    1. Knowable – Something went wrong in the first calculation. E.g. you asked Thomas the question but sneezed while he replied , rendering you deaf momentarily. Simply asking him again will get you the answer.
    2. Unknowable – The bar burned down and Thomas skipped town. There is no realistic way of getting the answer. (Please don’t poke holes in this, but I know you will)
    3. Not applicable – See the example above regarding the three children, but, bars and Thomases instead.

What does NULL mean here? Determining what NULL means must be performed on a case by case basis. This is why I believe it is often better to use an Enum or other structure as it more gracefully exposes the intention behind a non-binary return value.

Here is a rough example of what could be done:

Thanks for reading!!!

XML to Key Value Pairs (Works well for exporting data to Excel)

On a recent project, I had to provide export functionality for a web grid’s data source that contained wildly varying data structures. The XML structure contained anywhere between 1 to 10 levels of nesting and we needed to be able to export this data into an Excel or comma separate values file.

This code will show you how to turn a complex XML tree structure into a list of key-value pairs.

For example, here is a relatively simple data structure:

In order to get the structure ready for data export, this code turns the XML into a list of key value pairs.

And the console app…

Here is the output:

output