<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Alan Northam : ADO.NET</title><link>http://devlicio.us/blogs/alan_northam/archive/tags/ADO.NET/default.aspx</link><description>Tags: ADO.NET</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>ADO.NET, Nullable Types, Casting DBNull and You</title><link>http://devlicio.us/blogs/alan_northam/archive/2008/03/06/ado-net-nullable-types-casting-dbnull-and-you.aspx</link><pubDate>Fri, 07 Mar 2008 03:21:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:39575</guid><dc:creator>anortham</dc:creator><slash:comments>10</slash:comments><description>&lt;p&gt;I know this may be a little too scary for some of you so I apologize in advance.&amp;nbsp; Take a deep breath and try to imagine that you can&amp;#39;t use NHibernate and you&amp;#39;re stuck using ADO.NET (circa 2001, did they even have computers back then?).&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you&amp;#39;ve ever tried to read the value from a column in a datarow, you&amp;#39;ve probably run into something like this (a quick Google search will bring up page after page of variations):&lt;/p&gt;
&lt;p&gt;
myValue = (int)row[&amp;quot;ColumnName&amp;quot;]; //wait, what if it&amp;#39;s null?
if( row[&amp;quot;ColumnName&amp;quot;] == DBNull.Value)
    myValue = 0; //or maybe null if myValue is nullable int?
else
    myValue = Convert.ToInt32(row[&amp;quot;ColumnName&amp;quot;]); 
//lot of code for something simple, hope I don&amp;#39;t have to do this too many times...
//what about
if(row.IsNull(&amp;quot;ColumnName&amp;quot;))
//blah blah blah
&lt;/p&gt;
&lt;p&gt;
Wow, that&amp;#39;s ugly.  What if I have several nullable columns?  What if I have a lot of nullable columns?  There has to be a better way.
&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s suppose I have a class MyClass (original huh?) and it has a nullable DateTime property and a nullable integer property.  
&lt;br /&gt;
//snip from MyClass
public DateTime? Property1;
public int? Property2;
//end snip
//some contrived ado.net code
List&amp;lt;MyClass&amp;gt; list = new List&amp;lt;MyClass&amp;gt;();
foreach(DataRow row in dataTable.Rows)
{
    MyClass instance = new MyClass();
    instance.Property1 = row[&amp;quot;Column1&amp;quot;] as DateTime?;
    instance.Property2 = row[&amp;quot;Column2&amp;quot;] as int?;
    list.Add(instance);
}
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
I like a good one line solution.&amp;nbsp; Of course, you really can&amp;#39;t cast DBNull.Value (which would be the value of row[&amp;quot;ColumnX&amp;quot;]&amp;nbsp; if the column is null in the database) as a nullable int (or DateTime?).&amp;nbsp; (edit: thanks Chris) The cast above is actually failing but casting with &amp;quot;as&amp;quot; doesn&amp;#39;t throw an exception and returns a null value which is just what we want.&lt;/p&gt;
&lt;p&gt;&amp;quot;as&amp;quot;... you&amp;#39;re my hero...*sniff*&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=39575" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/alan_northam/archive/tags/Casting/default.aspx">Casting</category><category domain="http://devlicio.us/blogs/alan_northam/archive/tags/ADO.NET/default.aspx">ADO.NET</category><category domain="http://devlicio.us/blogs/alan_northam/archive/tags/Nullable+Types/default.aspx">Nullable Types</category></item></channel></rss>