In Response to Sahil Malik's Null problem

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?

Comments:

I think I've been misunderstood, I should have been clear in what I was trying to acheive.

Here is the problem statement --

By using the "Expression" on a datacolumn only, I'd like to restrict the decimal precision to 2 digits. (i.e. a money data type).

I'm sorry I confused the issue, my blog is a little sleepy right now (thanks to DNJ's sucky blog engine), but I'll try and fix that when I can.


 



 
Save Cancel