Rants and Raves
Thanks for visiting my blog!
In Response to Sahil Malik's Null problem
January 25, 2005
Url: http://dotnetjunkies.com/WebLog/sahilmalik/arch…
In response to your rant about the inelegance of the CLR’s null implementation:
I think you’re off track here. ADO.NET mirrors the same type dissonance that exists between SQL Datatypes and most programming types. While Nullable<> will help, it isn’t a panecea. We will still need to check for null (or DbNull) values. In your example, I would have:
DataColumn dc = new DataColumn() ;
dc.Name = "TotalCost" ;
dc.type = typeof(money) ;
dc.Expression = "price * quantity" ;
Because this value could (DbNull == true), the resulting value of (price * NULL) is NULL just like in SQL. You could get around this by providing default values, but that just extends the problem.
This seems like an elegant solution…just check for IsNull first:
if (dataRow[“price”] == DbNull.Value)
{
// ...
}
Or do it in your expression:
dc.Expression = “isnull(price, 0, price) * isnull(quantity, 0, quantity)”;
Does this make any sense?
This work by
Shawn Wildermuth
is licensed under a
Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License
. Based on a work at
wildermuth.com.