Wednesday, October 13, 2010

WPF DataGrid Entity Framework 4.0

Tags: WPF, DataGrid, Entity Framework, Detached

When I populate the datagrid with linq + entity framework for example

myModel context = new myModel();
myDataGrid.ItemsSource = from a in context.Apples select a;

and then you create a new record via the DataGrid and you want to handle the event InitializingNewItem. You may want to check what the display order of apples is, you can not check via context as the record/s are detached so you have to look in items. The problem is that the new record is of type MS.Internal.NamedObject not Apple.

So you write the following LINQ query
var MaxDisplayOrder = (from Apple a in myDataGrid.Items select a.DisplayOrderNo).Max();

You run it and get the following error exception:
Unable to cast object of type 'MS.Internal.NamedObject' to type 'Apple'.

To solve this problem use OfType method
var MaxDisplayOrder = (from a in myDataGrid.Items.OfType<Apple>() select a.DisplayOrderNo).Max();

Monday, May 31, 2010

.Net 4.0 Framework GAC Location

The location of the GAC has split with .Net 2.0 – 3.5 Framework remaining where it is
C:\Windows\assembly
And the .net 4.0 GAC location is now
C:\Windows\Microsoft.NET\assembly

Monday, May 24, 2010

VirtualBox using Hyper-v VHD image

Tag: Virtual Box Hyperv
I was sick of using windows server 2008 r2 and wanted to move to window 7(now 10). After reading a few reviews it was clear that VirtualBox works very nicely. So I nstalled VirtualBox on a clean windows 7  machine and copied across a VHD. Setup the virtual machine to use an existing hard drive.
image 
Select Next.
Select Memory.
Select Use an existing hard drive.
Select Add icon and select the desired VHD.
image 
Upon starting the virtual machine I received the following blue screen of death:
image
Error Message A problem has been detected and windows has been shut down to prevent damage to your computer.
If this is the first time you’ve seen this stop error screen, restart your computer. If this screen appears again, follow these steps:
Check for viruses on your computer. Remove any newly installed hard drives or hard drive controllers. Check your hard drive to make sure it is properly configured and terminated. Run CHKDSK /F to check for hard drive corruption, and then restart your computer.
Technical information:
*** STOP: 0x0000007B (0x80786b58, 0xC0000034, 0x00000000, 0x00000000)
Solution
The issue is that hyper-v will only build a bootable IDE hard drive. So when you create your virtual machine you need to make sure that you are not booting the VHD under a SATA Controller
image
but instead use the IDE Controller
image