28 July 2011

IE - Using onClick on a span element to control a textarea

I created a toolbar from span elements with background-images to control the contents of a textarea below it through the onClick event. This works fine in Opera and other browsers, but of course not in Internet Explorer. It seems IE looses the selection in the textarea when the span is clicked. There are two workarounds though:

Make the span unselectable

The first one is to simply make the span tag unselectable by adding
unselectable="on"
to it.

Use the onMouseDown event

Alternatively you can also use the onMouseDown event rather than the onClick event. This way the selection won't be lost yet when your JavaScript code does it's thing. But this on of course does slightly change the behaviour, and thus I prefer the first method.

07 July 2011

.NET - Foreach loop through a Dictionary

Looping a Dictionary<key, value> list using C#.NET is easy enough.
You just need to know how, and this is how:
private Dictionary<int, Customer> DataSupport { get; set; }

foreach (KeyValuePair<int, Customer> customer in DataSupport) {
  int key = customer.Key;
  Customer value = customer.Value;
}

.NET - Read/write a file line by line

To read some file line by line using C#.NET, I used this function:
if (File.Exists(App.CUSTOMERS_FILE_NAME)) {
  using (var reader = new StreamReader(App.CUSTOMERS_FILE_NAME)) {
    string line = string.Empty;
    while ((line = reader.ReadLine()) != null) {
      //Process the line
    }
  }
}

writing such a file is just as easy:
using (var writer = new StreamWriter(App.CUSTOMERS_FILE_NAME, false, Encoding.Unicode)) {
  writer.WriteLine("Whatever value");
}
The file will be automatically created if it doesn't exist yet.

04 July 2011

.NET Entity Framework - The version of SQL Server in use does not support datatype ‘datetime2′

I developed an application using the .NET Entity Framework and SQL Server 2008 on my local machine. When I moved it onto the production server, which runs SQL Server 2005, it threw this error as inner exception when trying to save a DateTime:
The version of SQL Server in use does not support datatype ‘datetime2′.
The solution is changing a setting in the Entity Framework to target SQL Server 2005 instead of 2008. This setting is not available through the IDE, so:
  1. Open the *.edmx,
  2. Find the ProviderManifestToken attribute on the Schema tag,
  3. Change it's value from 2008 to 2005,
  4. A rebuild might be necessary.