Having just help another developer with this issue, I thought I’d write a quick post about the cause of the ObjectDisposedException exception when working with Entity Framework.

It is important to be aware of two things when working with Entity Framework

1) The data context must not be disposed of until all the data is fetched from the database.

2) A linq statement creates an IQueryable, which is not executed until you enumerate over it (e.g. use it in a for each loop) or convert it to a list or an array.

Lets see and example:

Public Function GetProducts() As IEnumerable(Of Product)
    Using entities As New AdventureWorksEntities
        Return From p in entities.Products Where Not p.IsDiscontinued
    End Using
End Function

Now, If we call the above function like this:

For Each product In GetProducts()

We will get an ObjectDisposedException exception because the query is not executed until we get to the For Each loop, whereas the data context is disposed of before leaving the GetProducts() function.

To avoid this problem simply call .ToList() on the query inside the Using block so it is executed there and then:

Public Function GetProducts() As IEnumerable(Of Product)
    Using entities As New AdventureWorksEntities
        Return (From p in entities.Products
                Where Not p.IsDiscontinued).ToList()
    End Using
End Function

Unlike the release of VB.NET with .NET 4.0, the next release of VB.NET doesn’t have the anything like as many new features, which is almost certainly because the language is really maturing and a lot more parity has been achieved between C# and VB.NET.


The big new feature for both languages is the introduction of the await/async keywords. I won’t go into detail here because they are covered in lots of other places (including the VB.NET team blog).


One if the freebees we get because of the Async feature is the Yield keyword. This has been a feature of C# for a long time, and although not massively useful, when you do need it, it can save you a lot of time. Let look at an example.

Imagine you have a function that takes an array of integers and returns an IEnumerable(Of Integer) containing the even numbers in that array (I know – completely contrived demo). The easiest way to do it would be to build a list and then return it once it is complete, for example:

Function GetEvens(numbers As Integer()) As IEnumberable(Of Integer) 
  Dim evenNumbers As New List(Of Integer)

  For Each number in numbers
    If number Mod 2 = 0 Then
    End If

  Return evenNumbers
End Function

With the Yield keyword we no longer need the List to hold the results, we can return them (yield them) as we find them, making our function look like this:

Function GetEvens(numbers As Integer()) As IEnumberable(Of Integer)
  For Each number in numbers
    If number Mod 2 = 0 Then
      Yield number
    End If
End Function

The Global keyword has existed for a while to allow you to be explicit about which namespace you want. For example, assume you have this namespace in your project:

Namespace Acme.System.IO

If you have Imported the Acme namespace this line becomes ambiguous:

Dim dataFile As System.IO.File

The global keyword allows us to be explicit:

Dim dataFile As Global.System.IO.File

In the next version of VB.NET we will also be able to use the Global keyword in namespace declarations (for exactly the same purpose). Kind of minor, but useful when you need it.


There might be a few more language features announced, but that’s it for now…

One of the blogs I read is Zain Naboulsi’s Visual Studio Tips and Tricks. In a recent post he discussed the power of using Find and Replace with regular expressions and it gave me an idea about how to quickly reverse a set of assignment statements.

Lets say you have the following code:

Me.FirstNameTextBox.Text = customer.FirstName
Me.LastNameTextBox.Text = customer.LastName
Me.TelephoneTextBox.Text = customer.Telephone
Me.EmailTextBox.Text = customer.Email 

And you want to reverse the assignments so that you are writing the textboxes back into the customer object (generally the next thing you would want to do). Rather than doing it by hand, or using some macro, you can use the find and replace window with a regular expression:


This works because the expression matches any character (.) zero or more times (*), followed by an equals sign then any character zero or more times. In other words, anything followed by “=” followed by anything. The two anythings in the expression are enclosed with curly brackets to make them tagged expressions, which means they can be used in the replace expression. \1 will be the first tagged expression, \2 then next and so on. So our replace with expression just says replace a=b with b=a.

So what do you end up with? Well, this of course:

customer.FirstName = Me.FirstNameTextBox.Text
customer.LastName = Me.LastNameTextBox.Text
customer.Telephone = Me.TelephoneTextBox.Text
customer.Email = Me.EmailTextBox.Text

kick it on

If like me, you have to turn your hand to design every now and then, you might find some of these useful:



Free for commercial use fonts.

More free fonts



A very cool flash based colour picker.

Another colour scheme design – all HTML this time…



A library of icons with various licences.

Another library


Hope you find some of these useful… leave a comment if you have any other suggestions.

kick it on

I’m not a big fan of #region, I use it occasionally but generally try to avoid it. It’s always frustrating to open a code file and be presented with nothing but collapsed regions – sure, it looks neat (and lets face, more than a few programmers are a little OCD) but I want to see the code, that’s why I opened the file in the first place!

Don’t worry, I’m not going off on a rant, I just want to direct you to a much more level headed explanation of The Problem With Code Folding. I couldn’t agree more.

I saw this on Julie Lerman’s blog and had to give it a go. I’m quite relieved to say:

54% Geek

I’m glad to be a bit geeky, but wouldn’t want to score too high!!!

I ran into a page on the Microsoft web site that was showing a compile error. It’s fairly unusual but the developers over at MS are only human and they are going to make mistakes sometimes. What I thought was particularly interesting was that the CustomErrors option in their web.config was set to Off, showing the full source code of the page.

The page is at but I’m sure they will have fixed it by the time you read this so here is a screen shot.




I wish I could report some shocking fact gleaned from their source code, but it was all mundane C#…

I spent ages this morning trying to find a way to determine if a user has permission to access a page before navigating to it. I knew it was possible, the SiteMapProvider has a property called “SecurityTrimmingEnabled” that hides pages that the user cannot access.

In the end I found a really useful object called UrlAuthorizationModule in the System.Web.Security namespace that has a method called CheckUrlAccessForPrincipal(). Pass in the url and get a Boolean return value indicating whether the resource is accessible. Easy when you know how!

A while ago (in fact, it was two mobile phones ago) I had a non-touch phone. Most apps worked well but every now and again I wished I had a point and click interface. As I was trying to scroll around a particularly large web page I had an idea: What if the phone screen was a window and you scrolled it around by moving the phone itself? Almost all phones have a camera on the back, surely it could be used in the same way as an optical mouse to track movements on a surface. All you need to do is put your phone on the table and you could look at a large document as though it was stuck to the table itself!

I’ve tried to illustrate how it would work with the image of the bing home page below, except you would only be able to see the page through the phones screen (obviously).




I’m about to get a new phone and I’m going back to using a keypad (in fact I’m getting the Samsung from the image above). This has reminded me of my idea from years ago so I thought I’d search to see if anything like it has been done. It seems somebody has turned their Nokia mobile into a Bluetooth mouse for their pc, so it must be possible – come on Microsoft, this would be a great feature in Windows Mobile 7!!!

kick it on

Having written about the new features planned for VB.NET 10, I’ve been meaning to write about what to expect in ASP.NET 4.0, specifically what is happening around webforms. Well, Mike Ormond has written an excellent blog post on just that subject so I like to him instead:

ASP.NET 4.0 Webforms Enhancements