mha.dk

Random thoughts of wisdom - the personal blog of Michael Holm Andersen

SubSonic LoadFromPost method

I'm a big fan of SubSonic and use it in almost all my Web Applications. However, yesterday I discovered yet another nice feature of SubSonic - the LoadFromPost() method. What is does is it iterate over the the Request.Forms collection and matches the name of any form element with the corresponding property for your Database Object.

This means that you can now fill your entire Object using a single line of code, like this:

Customer cust = new Customer
cust.LoadFromPost(); // Load all the properties!
cust.Save();

This will save me hours of tedious code like:

Customer cust = new Customer
cust.FirstName = txtFirstName.Text
cust.LastName = txtLastName.Text

...
...
cust.Save();

Another bonus is that I don't have to adjust my code-behind if additional columns is added to the Table (Object) - I simply rebuild the Model using SubCommander and add additional Server Controls to my form (.aspx page). It's also possible to use plain html tags, so I don't have to worry about ASP.NET adding all the extra naming container stuff, and furthermore it will work perfectly with any JavaScript or CSS that uses the id of an html tag.

Additional media for your Vista license

It's possible to get your Windows Vista license on an altenative media, and even convert your 32-bit OEM version to a 64-bit! - Check out this site for more information: http://www.microsoft.com/windowsvista/1030/ordermedia/da-dk/default.mspx

Open Source ASP.NET e-commerce

Just a quick note to all your .NET geeks out there, who might be implementing e-commerce solutions - Check out dashCommerce, an ASP.NET Open Source e-commerce application much like OScommerce for PHP. I havn't tried dashCommerce yet, but if anyone gives it a go, please post a comment and tell us what you think.

dashCommerce can be found at CodePlex and at the dashCommerce Community Website.

20 years old - and still going strong!!!

As some of you might know I started programming computers at the age of 12 on a Commodore Vic20, and after a couple of months moved on to the mighty Commodore64 (C64) where I did a lot of coding together with my very good (and oldest) friend Lars (LSP).

Together we formed DGS (Danish Game Smashers) where we improved our coding, hacking, cracking and other "nerdy" skills for several years by coding demos, attending parties and developing games (yes, we actually did several commercial games on the C64). Lars called himself "Terraman" and I came up with the name "Mr. Zork" (or just Zork .. so now you know why my BF2 profile is called "MHAZORKY" :-) ...after a few years on the C64 we moved on to code the Amiga, but that's another story (and maybe stuff for a future blog post).

However today Lars found a couple of our C64 demos on The C64 Demo Portal where it shows that our demos is still being downloaded!! So, 20 years AFTER we did the demo "Bodyshocker", it's still being downloaded (probably by old, sentimental C64 geeks!!). Until now it's been downloaded 689 times this year .. I simply find it amazing , that code we did 20 years ago, someone still find interesting enough to download and run/watch on a C64 emulator - I wonder if that will be the case with the code we're writing today?!

Ternary Operators in C#

The C# language has a 'shortcut' to the simple if-else statement, called Ternary Operators. In short, it provides a way of reducing code like this:

if (MobilePhone != string.Empty)
{
  txtGSM.Text =
MobilePhone;
}
else
{
 
txtGSM.Text = "No number specified";
}

to this syntax:

txtGSM.Text = MobilePhone != string.Empty ? MobilePhone : "No number specified";

It takes a little getting used to, but actually it's quite nice after a while .. and it does make the code a lot less "messy".

The next thing you might want to do with Ternary Operators is to create code like this:

h < a ? h++ : a++;

However, this produces the error:  Only assignment, call, increment, decrement, and new expressions can be used as a statement.

The tricks is in the initial assignment, so the statement has to been adjusted to this:

int m = h < a ? h++ : a++;

The joy of Vista Preview Pane

 

One of the best (and probably most overlooked features of Vista) is the "Preview Pane" (in Explorer Menu: Organize > Layout > Preview Pane), which makes it really easy to see the content of files without having to open the actual Application (as a sidenode, I saw a video yesterday about the new Leopard OS which contains two "new" and "innovative" features called "Cover Flow" and "Quick Look", which is - yup, you guessed it - Preview of files directly in Explor... sorry Finder :-) .. so much for Apple comming up with new ideas!)

Anyway... if you're running Office 2007, you're able to preview Word, Excel etc. file formats (don't know if this also works if you're using Office 2003). Sadly a format which is not supported native by Vista is PDF.

Luckily Tim Heuer and Foxit (by far the fastest PDF reader for windows, check it out!) have joined forces and put together a PDF preview handler which can be downloaded right here: FoxitPdfPreviewHandlerSetup.msi (1,68 mb)

If you're into creating your own Preview Handler, take a look at the this article from MSDN Magazine.

Automatic script backup of your databases

Continuing on the backup article I wrote yesterday, I'll show you how I do a daily script backup (.sql) of all the databases on my development machine. Before we take a look at the script, we'll need a couple of utilities (don't worry, they're free!).

Database Publishing Wizard (part of SQL Server Hosting Toolkit)
WinRAR (you probably already got this one installed!)

Now.. let's take a look at the script - it goes something like this: 

echo off
cls
echo ***************************************************************************
echo ** Script all objects in databases and save them in 'dd-mm-yyyy' folder  **
echo ***************************************************************************
cd C:\temp
C:
md %date%
cd %date%
"C:\Program Files\Microsoft SQL Server\90\Tools\Publishing\sqlpubwiz" script -C "Data Source=.\SQLEXPRESS;Initial Catalog=PlastLogistik;Integrated Security=True" PlastLogistik.sql
"C:\Program Files\Microsoft SQL Server\90\Tools\Publishing\sqlpubwiz" script -C "Data Source=.\SQLEXPRESS;Initial Catalog=SonofonAutomatiseretBrugerhaandtering;Integrated Security=True" SonofonAutomatiseretBrugerhaandtering.sql
"C:\Program Files\Microsoft SQL Server\90\Tools\Publishing\sqlpubwiz" script -C "Data Source=.\SQLEXPRESS;Initial Catalog=aspnethotel;Integrated Security=True" aspnethotel.sql
echo ***************************************************************************
echo ** RAR compress all .sql script files                                    **
echo ***************************************************************************
"C:\Program Files\WinRAR\WinRAR.exe" -ibck a PlastLogistik.rar PlastLogistik.sql
"C:\Program Files\WinRAR\WinRAR.exe" -ibck a SonofonAutomatiseretBrugerhaandtering.rar SonofonAutomatiseretBrugerhaandtering.sql
"C:\Program Files\WinRAR\WinRAR.exe" -ibck a aspnethotel.rar aspnethotel.sql
echo WinRAR has completed execution
echo ***************************************************************************
echo ** Delete all .sql script files                                          **
echo ***************************************************************************
del PlastLogistik.sql
del SonofonAutomatiseretBrugerhaandtering.sql
del aspnethotel.sql
echo .SQL files deleted

It's really simple, actually - Let's break it down:

1) I switch to the default backup directory, in the example "c:\temp"

2) A new directory with todays date is created (if you're using DK locals the format is dd-mm-yyyy)

3) Using the Database Publishing Wizard, I do a script backup of each database (only three in this example, add as many as needed!)

4) Using WinRAR and the switch "-ibck" to disable the GUI, a .rar file is created for each .sql file (you can also compress all .sql files to a single .rar file if you prefer!)

5) Last step is to delete all those nasty .sql files (of course a "del *.sql" could also be used to minimize script if you don't store additional .sql files in the directory!)

Awesome FREE backup utility

I admit it .. I'm a 'backup fanatic' - In my pursuit of never loosing data, I use both RAID1 (mirroring), USB harddrives and NAS devices, plus "out-of-office" backup - and I seldom delete anything (leading to the ever-growing storage problem).

How do I do it then!! - Well first of all I'm using a very nice (and free) backup utility called Cobian Backup

Cobian Backup is simply put a "scheduled backup agent". It runs as a service being able to do scheduled backups to local, network and FTP locations. It does both full and incremental (based on archieve bit) backups and can compress the complete backupset info a single file using standard ZIP format.

My backup "process" looks something like this:

  • Daily (differential) scheduled backups of very important files, e.g. "inetpub" folder, Word/OneNote documents etc. to a RAID1 NAS device.
  • Every 14th day a complete HD backup is done to a RAID1 NAS drive, which is located in a 'hard to find place' (to avoid theft).
  • Every month a complete (approx. 500GB) "all-data-backup" is done and stored in a safe.
  • Every third month a complete (approx. 500GB) "all-data-backup" is done (rotation occurs!) and stored in a safe on a external location.

Needless to say - I've never lost a file! - Remember "Only dumb people or people whose data is worth nothing is careless about backup!!" - so hurry up, start taking those backups :-) 

Use arrow keys in Windows Alt-Tab task list



Windows Vista introduces small improvements in many aspects of the GUI - One I would like to point out is the ability (at last!) to use the arrow keys to navigate in the Windows Alt-Tab task list .. very nice, when you have a lot of applications running.

They've also changed the dialog so I don't need to scroll when I'm running more than 28 applications as the above image shows - finally!!

Check your (mobile) browser

As I work at a phone company, I'm getting more and more interesting in developing web applications for mobile phones / devices. Yesterday I put together a quick page which detects the capabilities of the requesting browser.

I you would like to check it out, it's right here: http://www.mha.dk/useragent.aspx