About strongtyped datasets, DBNulls, WPF and binding

The mix of strongtyped datasets, DBNull values, WPF and bindings will likely yield exceptions of invalid cast type when binding nullable columns.

Imagine you have a table with a single, nullable, column Description. You would bind it against a TextBox (or any other control) with this syntax: {Binding Description}. When binding is performed against a row that has a DBNull value (remember, ADO.NET uses DBNull.Value for null values) in that column you get an Unable to cast object of type 'System.DBNull' to type 'System.String' exception.

The reason for that exception is quite simple. Let’s take a look at how ADO.NET implements the strongtyped Description column:

   1: [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
   2: [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "")]
   3: public bool IsDescriptionNull() {
   4:     return this.IsNull(this.tableXXX.DescriptionColumn);
   5: }
   7: [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
   8: [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "")]
   9: public string Description {
  10:     get {
  11:         try {
  12:             return ((string)(this[this.tableXXX.DescriptionColumn]));
  13:         }
  14:         catch (global::System.InvalidCastException e) {
  15:             throw new global::System.Data.StrongTypingException("The value for column \'Description\' in table \'XXX\' is DBNull.", e);
  16:         }
  17:     }
  18:     set {
  19:         this[this.tableXXX.DescriptionColumn] = value;
  20:     }
  21: }

The obvious problem is that WPF binding goes straight to Description property without checking IsDescriptionNull() method (as your code should) and then property getter fails to convert DBNull.Value to a string. And no, at this point even a converter won’t help you because the value is read before converter is even invoked.

But fear not, the solution is a very simple one. Just add square brackets around Description in binding syntax, like this: {Binding [Description]}. Looks similar but it is different. The former syntax uses strongtyped property to access the data while the later uses the DataRow’s this[string] accessor instead which returns an object and thus no exceptions are raised due to the DBNull.Value to string conversion. Furthermore a DBNullConverter value converter can come handy as well:

   1: public class DBNullConverter: IValueConverter
   2: {
   3:     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
   4:     {
   5:         if (value == DBNull.Value)
   6:             return DependencyProperty.UnsetValue;
   7:         else
   8:             // return base.Convert(value, targetType, parameter, culture);
   9:             return value;
  10:     }
  12:     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  13:     {
  14:         if (value == null)
  15:             return DBNull.Value;
  16:         else
  17:             // return base.ConvertBack(value, targetType, parameter, culture);
  18:             return value;
  19:     }
  20: }

Migrating a Windows Home Server guest machine from VMWare Virtual Server 2.x to Hyper-V

I was running a Windows Home Server under VMWare Virtual Server as a guest machine. It had a dedicated 750GB hard disk to host a fixed size virtual disk spanning entire hard disk.

These days I am migrating this and other virtual machine to the Hyper-V 2008 R2 free server and here is how I did migrate it.

  1. Uninstall Virtual Server Tools from guest machine (this is important at this point in time because later it can’t be done easily through add/remove programs).
  2. Shut down guest.
  3. Copy all VMDK files to a spare (new) 1.5 TB Seagate disk. This step isn’t strictly necessary but it was for me because the source disk had troubles reading some sectors – if I wanted to proceed I had to have all the files on a good disk. This step took something like 4 hours over 1Gb LAN.
  4. Download and run VMDK to VHD Converter.
  5. Convert VMDK files (as input select the one without numbers if your virtual disk is partitioned across many files, i.e. SomeDisk.vmdk). I converted to the same hard disk (it barely fits) and it took something like 7 hours.
  6. Copy the resulting VHD to the Hyper-V server (I could pick the server as target location in step 5. but I felt more comfortable doing conversion locally). This step again took something like 4 hours.
  7. Create a new Virtual Machine on Hyper-V server, attach the resulting VHD file as its disk.
  8. Run the machine, activate OS (it will detect “huge” hardware change and it will require activation).
  9. Install Integration Services (Action/Insert Integration Services Setup Disk on connection window) and that���s it.

Lessons learned:

  • Hard disks are growing fast in size but network speed doesn’t. Thus such transfers will be slower and slower due to the sheer amount of data to transfer between disks.
  • Such operation might take whole day
  • If you use an external disk like I am then you should really stick with e-Sata instead of USB 2 or firmware (it is up to 4x faster)
  • Have enough free space on hard disks

An odd error when adding items to toolbox in Visual Studio 2010

Ever got an error like this when trying to add a toolbox item?


Textual representation (for search engines):

Microsoft Visual Studio
Could not resolve mscorlib for target framework '.NETMicroFramework,Version=v4.1'.  This can happen if the target framework is not installed or if the framework moniker is incorrectly formatted.

and here is another one:


Microsoft Visual C++ Runtime Library 
Runtime Error!

Program: C:\Program File…

An application has made an attempt to load the C runtime library incorrectly.
Please contact the application’s support team for more information.

The later one is really useful.

Note: It isn’t strictly linked to v4.1 – it happens with earlier versions as well.

Another side effect is that toolbox is somewhat corrupted, only certain tabs are loaded. But fear no more, here is the culprit and the workaround we found.

It turns out that there are problems when .net microframework is installed and its assemblies can’t be loaded.

Update (23.12.2011)

Microsoft wisely broke the link to the thread containing the solution - the solution thread has vanished into the black hole of the internet.
So, here is the solution as far as I remember :-):
Rename the files (or delete them) FrameworkList.xml which are installed in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETMicroFramework\v4.x\RedistList. Note that each .NET Micro Framework installs its own file in its own folder.

Comparing heating costs using Silverlight

Out of curiosity I’ve decided to compare actual heating costs versus theoretical heating costs. The calculation shows how much it would cost If I used a modern conditioner instead of a centralized heating provided by city and fuelled by natural gas. Hence I crafted a simple Silverlight application using DevExpress Silverlight components (which helped a lot, but that’s another story).

The default data is my data for January but you can enter your own. While the application is in Slovene you can pretty much understand the huge difference just by looking at the chart below. So much for naively believing that heat provided directly by fire is the cheapest one.

Follow this link to the heating cost calculator

Waiting for a tablet

It has been quite a while since I’ve started thinking about an e-Reader. The primary reason is that I have a bunch of e-books (many of them from Manning). I bought them with an e-reader in mind. However, they are in various formats, PDF among them.

The mainstream e-reader is still a 6” device with a single 9” out there - Kindle DX which is quite expensive and run by Amazon. This could be a worrisome fact – they demonstrated it twice at least: first they removed legally bought books from e-readers just like that (ironically those were Orwell’s) and they denied hosting Wikileaks on their cloud servers because of a single phone call made by a politician. Being at the mercy of such a company isn’t really something good, is it? Anyway, the thing is that 6” devices are suitable for displaying more or less e-books only, not all e-content (think PDF) due to the screen size. And e-readers with bigger screens are not coming yet, are they? During this time iPad happened  which shows that a tablet is a good alternative to e-readers if properly done (plus it can be used for many daily tasks where e-readers simply don+t work). Thanks for that, iPad. Hence I shifted my focus to tablets.

Here are my requirements for a tablet capable of displaying e-books in various formats, reading e-mail, surfing, etc:

  1. Real battery life at least 6hrs
  2. Screen resolution at least 1024x600
  3. Screen size at least 7” (not sure if I rather set it to 9” – have to check them in real world)
  4. Not being at the mercy of anybody for content installation
  5. Applications can be developed on PC (.net is preferred)
  6. A decent chipset (nVidia Tegra 2 looks the best bet right now)
  7. Open architecture with an option to install custom OS (when the manufacturer stops supporting it)

So let’s start with the eliminations.


I’ll never buy iPad due to the conflict with requirements 4, 5 and 7. My biggest problem with iPad is the Apple politics behind it. If you have it then you are at the mercy of Jobs who decides what are you allowed to run, install, share and read - Apple censors content for no reason, at least they don’t even try to explain it, Wikileaks application was one of them. From the hardware point of view it is a fine device though.

Windows 7 tablets aka slates

There are some and come in various forms and factors. However, none of them makes it over my first requirement. They are slow, too - thanks to Windows 7 not being optimized for tablet format. I could survive slowness but not being able to run over 3hrs straight is a complete showstopper. Imagine, you just boot Windows 7, open Word and your document within it and you have to recharge already. It is beyond me why the manufacturers even bother producing them. The only bright point here is that Microsoft announced support for ARM and SoC with forthcoming Windows 8. I guess I’ll have to re-evaluate Windows based tablets in two years or more.


No idea.


Which leaves the Android based tablets as the only alternative. Currently the only decent one is Samsung Galaxy Tab which is heavily overpriced. It is more expensive than iPad even though it features much smaller display (7” compared to iPad’s 9”) and a free open source OS. Furthermore it is running Gingerbread (or is it Froyo) without any upgrade commitment from Samsung (you can’t just use stock Android due to Samsung’s customization).

The real deal are many Tegra 2 based tablets running Honeycomb announced at CES and I guess many more are coming in the near future. I think this spring there will be enough tablets on the market to choose from. I don’t know yet which one I’ll buy but I am skeptic about Motorola and HTC. The first one because of Milestone fiasco .They released the same hardware as Droid in the US which is a Google reference device. Meaning Google support and stock Android. But they released its twin as Milestone in EU as completely closed system with rare and very late updates (requirement 7) as if EU doesn’t deserve and open device. Screw you Motorola. Nor do I trust HTC which, rather than supporting their phones or putting some effort and care into them, releases new and new models almost monthly. Not saying others are better or worse but I just don’t trust these two.

To be honest Samsung is very bad at supporting as well. Just look at their flagship Galaxy S phone which could be much better. Having a top notch hardware is FUBAR slow due to the incredible stupid decision to use RFS as their file system. You have to use one of the lagfixes to see it fly. OS customization bloat named Touch Wiz doesn’t help with upgrades - The OS upgrade speed shown so far is poor – upgrading to Froyo took them around 6 months.

I don’t know about other manufacturers but I assume they can be even worse. Hence I’d really appreciate a tablet running a stock Android or the possibility to install a stock Android. This way updates would be piece of cake. This is valid for phones as well, not just tablets.

I am still optimistic for this spring. Am I too demanding?

Righthand DataSet Visualizer goes 1.0

As a New Year’s gift I’ve fixed few bugs in Righthand DataSet Visualizer and set its version to 1.0.

Here are some changes:

  • uncompressed assembly is a bit larger due to avoiding compression within merged assembly.
  • RowState is visible again
  • some grid drawing related fixes

Read more about Righthand DataSet Visualizer here.

Download the newest and older versions from download section.

2008 version is here.

2010 version is here.

Capture DirectX 10/11 debug output to Visual Studio

Working with default DirectX configuration in a Visual Studio project is like working with a black box. Even more so when you have a managed code project. Mostly you’ll get an ArgumentException saying “Additional information: Value does not fall within the expected range.” or something like that, almost totally not helpful. But fear not, there is a way to capture a ton of useful information right into Visual Studio’s Debug output window. And here is how:

0. I assume you have DirectX SDK already installed.

If not download if it from DirectX Developer Center.

1. Enable debug output in DirectX control panel

a) Go to Start/All Programs/Microsoft DirectX SDK ([Month] [Year])/DirectX Utilities [(64-bit)] and run DirectX Control Panel [(64-bit)]


b) Go to Direct3D 10.x/11 tab. Except for the Edit List… button everything is disabled. The reason is that you have to add applications you wish to debug beforehand you can alter the settings (this is not obvious and UI is really clumsy here).


c) Click Edit List… and add your application to the List of process or folders. Clicking on […] button and selecting application exe file does the trick. Click OK button to close this window.

d) Next step is to actually enable debugging of the application. This can be done in two ways – either select Debug Layer’s Force On or switch on debugging directly from code (shown later in step 2.). You can select which messages won’t be displayed through Mute settings.


2. Alternatively to enabling debugging from step 2d)

You can create the DirectX device in code with CreateDeviceOptions.Debug option, like the code below when using managed code and Windows API Codepack:

D3DDevice device = D3DDevice.CreateDevice(null, DriverType.Hardware, null, CreateDeviceOptions.SingleThreaded | CreateDeviceOptions.Debug, levels);

This option will work with both Applications Controlled and Force On Debug Layer settings but not with Force Off.

3. Enable unmanaged debug output in Visual Studio project

The final step is to allow showing unmanaged debug output in Visual Studio debug output window. This works by default in an unmanaged project but not in managed projects. Note that it is a per-project setting.

Open project properties, Debug tab and make sure that Enable unmanaged code debugging option is checked.


Here you go. You’ll see plenty of DirectX messages in Debug output window from DirectX such as:


Disabled post comments due to spam

Due to the massive spam amount I was getting these days I’ve opted to disable all comments to my posts for the time being. When I figure out a proper anti-spam system I’ll re-enable them, perhaps through reCaptcha or something.

You can still send me the feedback through the Contact form.

Stay tuned.