[stringtemplate-interest] DbNulls in untyped DataRow
Grzegorz Danowski
gdn at poczta.onet.pl
Tue Jun 26 09:24:20 PDT 2007
Hi,
I have used standard ADO.NET data containers (DataTables, DataRows etc). It
seems that they work well with String Template, but there is one exception
that I've found: problem with DBNull values. Example:
[TestMethod]
public void DataRowWithEmptyColumn()
{
DataTable table = new DataTable();
table.Columns.Add("CustomerName", typeof(string));
table.Columns.Add("Birthday", typeof(DateTime));
DataRow newRow = table.NewRow();
newRow["CustomerName"] = "Bill";
newRow["Birthday"] = DBNull.Value;
table.Rows.Add(newRow);
StringTemplate template = new StringTemplate(
"$data.CustomerName$ " +
"$data.Birthday; null=\"(unknown)\"$");
template.SetAttribute("data", newRow);
string expected = "Bill (unknown)";
Assert.AreEqual<string>(expected, template.ToString());
}
Above test fails (actual string is "Bill ").
Mean time (writing this post) I have solved my problem: I had used typed
dataset - in this case row has property IsBirthdayNull, my proper template
was:
StringTemplate template = new StringTemplate(
"$data.CustomerName$ " +
"$if(!data.BirthdayNull)$$data.Birthday$" +
"$else$(unknown)$endif$");
And it works! I like ST very much!
But I wonder if I can use DBNulls with untyped DataRow that has only
IsNull(string columnName) function?
I have tested an idea:
StringTemplate template = new StringTemplate(
"$data.CustomerName$ " +
"$if(!data.Null(\"Birthday\"))$$data.Birthday$" +
"$else$(unknown)$endif$");
Of course test was unsuccessful.
Regards,
Gregory
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20070626/0d5ee65b/attachment-0001.html
More information about the stringtemplate-interest
mailing list