Been coding a bit with Microsofts MVC3 web framework lately and ran into this error:

{"The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated."}

Apparently this is not an uncommon problem. There are quite a few forum threads about it but finding the reason or even better a solution was quite hard.

The reason:
The DateTime type in C# differs from the one that SQL uses.
C# can have null values and also accepts a wider span of dates, i.e: the date 0001/01/01 00:00 is ok in C#’s but throws the error above in SQL.

Solution
Solution one is to make sure you set all date variables to supported values (1753/1/1 to 9999/12/31) before you commit them to SQL.

The second option is to run this script on the Database after you rebuild it to replace the date type used on the columns to one that allows the same dates that C# does.

DECLARE @SQL AS NVARCHAR(1024)
DECLARE @TBL AS NVARCHAR(255)
DECLARE @COL AS NVARCHAR(255)
DECLARE @NUL AS BIT
DECLARE CUR CURSOR FAST_FORWARD FOR
    SELECT  t.name, c.name, c.is_nullable
    FROM    sys.tables AS t
    JOIN    sys.columns c ON t.object_id = c.object_id
    JOIN    information_schema.columns i ON i.TABLE_NAME = t.name AND i.COLUMN_NAME = c.name
    WHERE   i.data_type = 'datetime' and t.name not like '%aspnet%'

    ORDER BY t.name, c.name

OPEN CUR
FETCH NEXT FROM CUR INTO @TBL, @COL, @NUL
WHILE @@FETCH_STATUS = 0
BEGIN
    SELECT @SQL = 'ALTER TABLE ' + @TBL + ' ALTER COLUMN ' + @COL + ' datetime2' + (CASE WHEN @NUL=1 THEN '' ELSE ' NOT' END) + ' NULL;'
    EXEC sp_executesql @SQL
    FETCH NEXT FROM CUR INTO @TBL, @COL, @NUL
END

CLOSE CUR;
DEALLOCATE CUR;

I used a mix of the two. I first ran the script and then went though my database tables to find out which columns in the database I was setting with “incorrect” dates and made sure I set those properly.

Tagged with:
 

Here is my next attempt at a panorama, the image is 50 Megapixels (10000 x 5000 pixels), and close to 60Mb in size, so dont try to download a copy unless you’ve got a good internet connection
rockpano

Tagged with:
 

Went out on the first of many inlines runs this year and happened to bring my camera along, took a few pictures out of which these three turned out pretty ok. I like this first one the most.
mg_1668

Continue reading »

Tagged with:
 

Have been testing what Microsofts XNA framework can do and am toying with the idea of making some sort of Worms style game. Spent a few minutes today googling on how one can generate the terrain for such a game and found a few pages which explained parts of it but no place really contained any good working examples.
So after spending a few hours with the examples I could find I came up with an algorithm which works pretty well.

Here is an example of what the code generates:

wormsstyleterraininxna

This is example terrain was generated in about 2 second.

Continue reading »

Tagged with:
 

My office recently moved from a building in which the  rooms were were quite small and only a few people sat in each room to an office in which everyone sits in a large open space.

A concern we had with the new office was that the background noise would be quite loud when everyone is talking on the phone and conversing between each other.  We diden’t want to divide the room into smaller sections so one solution we had to this problem was to buy some sort of decibel detecting device that would warn when the sound level got too high. Unfortuately those devices are hard to find and quite expensive so I instead set out to see if I could build a cheap but just as good detector myself.

Here is a video of the result:

There were three problems to solve in order to get this to work.

(1) Sound Detection – How loud is it, and when is it to loud.
(2) Controll - some sort of switch board to open or close a channel when sound is to loud.
(3) Warning – Making the switch board turn on a stroboscope.

Continue reading »

Tagged with: