Sunday, December 28, 2008

kitten proof your Wii

I have previously written about the adventures of living with Luddite kittens. this Christmas presented a special challenge (pardon the pun). under the tree arrived a Wii for my wife and me. we opened the box and then shared a look of horror--the Wii comes with an LED sensor bar that's powered by a thin cable. we knew instantly that this cable would not last a day in the same household as our cats.

we have tried the usual precautions, but none have worked for our tag-team of furry felines. for example, duct tape with the sticky side out keeps Rory away, but Asha likes chewing on sticky things, so her gnawing exposes the wires for Rory. it's hard to believe that such adorable creatures could cause any trouble at all...

- Asha (the oldest)

- Rory (the baby of the family and the proud owner of razor sharp teeth)

and, of course, we got caught up in a rousing game of Wii tennis and took our eye off Rory for the few seconds she needed to make mince of the cable. after splicing it back together, I knew that it was time for some more precautions. we went to the hardware store and bought a healthy supply of Split Flex Tubing (about $12 for 20'). it's simply a flexible tube that we are putting around any cable that seems fine enough for Rory's taste--fortunately, she stays away from thicker cables.

in addition to the Wii's sensor cable (which we'll only use until we can get a wireless one), we've put the tubing around the thin part of the Wii power cable, the power cable for the Xbox 360 add-on fan, and the small cable that comes with the Xbox 360 wireless network adapter--the router has long since been moved into a cat-free room.

- the Wii power cable protected from harm

- the Xbox 360 wireless network cable with a protective cocoon

so far, I'm very happy with the tubing. I think it might save me from some ill will towards our pets. on that note, I'm currently reading Marley and Me, which I'm also hoping will put our cats in a positive light.

XML indentation not working

I'm using an XML file to store a configuration file for the Metalogix Batch Migration Utility. during a recent bug bash, I noticed that the config files were not being properly indented despite the fact that I was setting System.Xml.Formatting.Indented in my code.

// Create an XML writerSystem.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(1000);
System.IO.StringWriter stringWriter = new StringWriter(stringBuilder);
System.Xml.XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.Formatting = System.Xml.Formatting.Indented;
xmlWriter.Indentation = 5; //optionally adjust the indentation
// Write out configuration settings
xmlWriter.WriteRaw("" + txtDBFile.Text + "");

it turned out that the issue was simply that I was using xmlWriter.WriteRaw instead of xmlWriter.WriteElementString. the latter will respect System.Xml.Formatting.Indented and is cleaner code anyway.

to crush this bug, all I had to do was replace the last line (and other similar lines) with this:
xmlWriter.WriteElementString("DBFile", txtDBFile.Text);

Wednesday, December 10, 2008

.NET ComboBox shows data type

I've been using a data bound ComboBox in one of my apps, and I discovered that under certain conditions, the ComboBox would display "System.Data.DataRowView" instead of my values. in other words, it was showing the data type instead of my content.

in this case, I'm binding an XML file to a DataSet and then showing the content in a ComboBox (C#). the issue occurred when I had more than one child element in the XML.

this is my original (non-working) code (xmlQueries is a DataSet object):

comboBoxXpath.ValueMember = "XpathQuery";
comboBoxXpath.DisplayMember = "XpathQuery";
// Note: set the datasource after the properties
comboBoxXpath.DataSource = xmlQueries.Tables[0];

thankfully, the Visual Studio debugger showed me the light, it turned out that the DataSet column name was changing (to "XpathQuery_Text") when I had more that one value in my XML. the solution was to read the column name dynamically:

// Read column name dynamically
comboBoxXpath.ValueMember = xmlQueries.Tables[0].Columns[0].ToString();
comboBoxXpath.DisplayMember = xmlQueries.Tables[0].Columns[0].ToString();
// Note: set the datasource after the properties
comboBoxXpath.DataSource = xmlQueries.Tables[0];