November 2005 - Posts

DataSet Default Value Woes
In their MSDN help topic DataColumn.DefaultValue Property  Microsoft glibly advertise that :

A default value is the value that is automatically assigned to the column when a DataRow is created (for example, the date and time when the DataRow was created.


Now if this isn't skirting an issue I don't know what is.  In this example, the DateTime value that is actually assigned to the column when a DataRow is created is the DateTime value at the time the DefaultValue property was set.  This value persists until the property is set again, and the only way to dynamically assign defaults, such as the current date and time, or a new GUID, etc. is to set a fixed dummy value for the DefaultValue property, to prevent null values stopping an insert, and then asisgn the real value after the row is created, in the RowChanged event.

Call me fussy, but I find this a horribly clumsy way to perform what is quite a common task.
Pretty XML
OK, its not really pretty, but at least indented and readable.  I need to compare XML files like source files, with semantic lines, so I set out to develop an XML indentor[1].  My first run was using an XmlTextRead and a StringBuilder (with its useful AppendFormat method), but the intricacies of different node types made me lazy.  Progress on my side, I decided to use an XmlTextWriter to write exactly the correct node type for each node the reader visited, but still manage my own indents, using strings of tabs according to level. 

It now seems logical, but I wonder how many people know how hard it is to find out how to generate a string of characters in a given length, like the VB String() function.  MSDN is breathtakingly opaque on this, and to this day some people just roll there own in a loop, or use say PadLeft on an empty string.   I only found the proper way tonight and will be imparting the secret to the first ten beer carriers at my door.

Lo, in all the searching (you'd thing they'd include it as a common task topic somewhere) I saw mention somewhere of an Indentation property on an XmlTextWriter!  It was now about 2am but I was aroused!  Yes, you can set the writer to format its output, and you specifiy the character and length of each indent unit, and it manages the rest.  Wait, there is more!  Even before I wrote a single line using this new friend, I stumbled over the XmlTextWriter.WriteNode method.  This little baby takes an XmlTextReader, and writes it.  No startdocument end document etc.  Nothing, no loop, nada. 

Here is a very basic example with some limitations and ommissions, but you'll get the picture:

            XmlTextReader xtr = new XmlTextReader(fileIn);
            xtr.WhitespaceHandling = WhitespaceHandling.None;

            XmlTextWriter xtw = new XmlTextWriter(fileOut, xtr.Encoding);
            xtw.Formatting = Formatting.Indented;
            xtw.IndentChar = '\t';
            xtw.Indentation = 1;
            xtw.WriteNode(xtr, false);


The literals for the indentation are my overrides of the default 2 space characters.  I wanted really open readable output.

New Toy: Computed Columns
I was doing a bit of prototyping over the weekend when I discovered this nifty little feature.  The SQL BOL for CREATE TABLE give this option:

CREATE TABLE
    [ database_name.[ owner ] . | owner. ] table_name
    ( { < column_definition >
        | column_name AS computed_column_expression
        | < table_constraint > ::= [ CONSTRAINT constraint_name ] }

            | [ { PRIMARY KEY | UNIQUE } [ ,...n ]
    
)

"....The expression can be a noncomputed column name, constant, function, variable, and any combination of these connected by one or more operators. The expression cannot be a subquery....."

Even in itself I found this very useful, and I easily overcame the exclusion of subqueries by using a User Defined Funtion, which is allowed.
GOTO GOTO
GOTO2 is a recursive cult in honour of our Creator, the Flying Spaghetti Monster, and aims to encourage a return to GOTO and good wholesome Spaghetti Code. 

Add a comment to express support, and who know, if there is enough support maybe I'll do some badges.
Djou Pa se Hol!
Johannes de Viliers got the wording right when he quoted preacher Peet Botha (he says Dr. Botha, but I think it's Ds. Botha, but who knows) in saying that the Anus is Holy.  ***, what a surprise!
Competition
Tell me why we think Humpy Dumpty was an egg, and win a free glass of water.
If your software were on a date...
Kathy Sierra has an amusing and informative article likening software behaviour to dating behaviour.
Mailing List for Web Developers
A new mailing list for web developers has opened today:

According to the blurb:

The Webdev list is for South African web developers (and friends of South African web developers) where they can ask all the questions they were too afraid to ask about web development (including but not limited to HTML, Javascript, PHP, DHTML, CSS2, XML, XHTML, ASP, .Net, back-end integration, design, scripting, security, DOMs blah blah blah).

 Subscribe here.