<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/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>DBNull and Nullable types</title><link>http://blogs.msdn.com/thottams/archive/2008/06/30/dbnull-and-nullable-types.aspx</link><description>When we have C# code interacting with the data base you run into this problem. Let me try and explain it. The database has nullable columns and C# has nullable types. For example, let us take a table Employee which has Id (int), name (string) and age</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: DBNull and Nullable types</title><link>http://blogs.msdn.com/thottams/archive/2008/06/30/dbnull-and-nullable-types.aspx#8672633</link><pubDate>Mon, 30 Jun 2008 22:41:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8672633</guid><dc:creator>Trygve Valoy</dc:creator><description>&lt;p&gt;You could solve this with an extension method.&lt;/p&gt;
&lt;p&gt;The extension method would look something like this:&lt;/p&gt;
&lt;p&gt;public static TValue GetNullableValue&amp;lt;TValue&amp;gt;(this DbDataReader reader, string name)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; object value = reader[name];&lt;/p&gt;
&lt;p&gt; &amp;nbsp; if (DBNull.Value.Equals(value))&lt;/p&gt;
&lt;p&gt; &amp;nbsp; {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; return default(TValue);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; }&lt;/p&gt;
&lt;p&gt; &amp;nbsp; return (TValue)value;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;And your data access code will be:&lt;/p&gt;
&lt;p&gt;int? age = customerReader.GetNullableValue&amp;lt;int?&amp;gt;(&amp;quot;Age&amp;quot;);&lt;/p&gt;
</description></item><item><title>re: DBNull and Nullable types</title><link>http://blogs.msdn.com/thottams/archive/2008/06/30/dbnull-and-nullable-types.aspx#8673211</link><pubDate>Tue, 01 Jul 2008 00:23:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8673211</guid><dc:creator>jackbond</dc:creator><description>&lt;p&gt;I've encountered the same ugly convention as well, and was thinking along the lines of an extension method similiar to the previous poster. Although he missed the part about not being a nullable type (easy enough to tweak his method to be similiar to SQLs IsNull function.)&lt;/p&gt;
&lt;p&gt;When it is a nullable type, I've done this in the past:&lt;/p&gt;
&lt;p&gt;int? age = dr[&amp;quot;Age&amp;quot;] as int?;&lt;/p&gt;
</description></item><item><title>re: DBNull and Nullable types</title><link>http://blogs.msdn.com/thottams/archive/2008/06/30/dbnull-and-nullable-types.aspx#8673889</link><pubDate>Tue, 01 Jul 2008 02:32:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8673889</guid><dc:creator>Steve Perry</dc:creator><description>&lt;p&gt;This problem (dbnull) has been around since well before .net and Microsoft has never done anything to make it any better, nullable types don't really bring us any closer to a way to map between a database null and a data type. &lt;/p&gt;
&lt;p&gt;Maybe the entity framework will finally fix this issue.&lt;/p&gt;
</description></item><item><title>re: DBNull and Nullable types</title><link>http://blogs.msdn.com/thottams/archive/2008/06/30/dbnull-and-nullable-types.aspx#8674237</link><pubDate>Tue, 01 Jul 2008 04:10:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8674237</guid><dc:creator>farrio</dc:creator><description>&lt;p&gt;I also noticed this issue when I was using VB2005 to retrieve the data from SQL2005. I created a class named DBNullable(Of T) to deal with it. But I think M$ need to do something to fix this problem because it's confusing and fussy when developing a data-based system.&lt;/p&gt;
</description></item><item><title>re: DBNull and Nullable types</title><link>http://blogs.msdn.com/thottams/archive/2008/06/30/dbnull-and-nullable-types.aspx#8674783</link><pubDate>Tue, 01 Jul 2008 06:34:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8674783</guid><dc:creator>Omer van Kloeten</dc:creator><description>&lt;p&gt;I love Trygve's idea, but it doesn't apply to DataSets (yes, some of us still need to use DataSets).&lt;/p&gt;
&lt;p&gt;...and farrio, re: 'M$', are you like 4 years old or something?&lt;/p&gt;
</description></item><item><title>re: DBNull and Nullable types</title><link>http://blogs.msdn.com/thottams/archive/2008/06/30/dbnull-and-nullable-types.aspx#8677078</link><pubDate>Tue, 01 Jul 2008 16:54:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8677078</guid><dc:creator>Trygve Valoy</dc:creator><description>&lt;p&gt;There's no reason why this shouldn't work for DataSets. You just need another extension method. You can even take the table name as a parameter in this method to resolve which table of the dataset you want to get data from.&lt;/p&gt;
</description></item><item><title>re: DBNull and Nullable types</title><link>http://blogs.msdn.com/thottams/archive/2008/06/30/dbnull-and-nullable-types.aspx#8679136</link><pubDate>Wed, 02 Jul 2008 02:43:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8679136</guid><dc:creator>thottams@microsoft.com</dc:creator><description>&lt;p&gt;I like the extension method idea as it will work for both nullable and non-nullable types. &lt;/p&gt;
&lt;p&gt;int? age = dr[&amp;quot;Age&amp;quot;] as int?; also works. It works because .NET is unable to cast it and assigns a NULL instead, which ends up being what we desire in this case.&lt;/p&gt;
</description></item><item><title>re: DBNull and Nullable types</title><link>http://blogs.msdn.com/thottams/archive/2008/06/30/dbnull-and-nullable-types.aspx#8680617</link><pubDate>Wed, 02 Jul 2008 10:59:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8680617</guid><dc:creator>Lothan</dc:creator><description>&lt;p&gt;I think this depends on whether you need to keep -1 as the default value or what you want to keep the null value. One possibility is to use the as operator for a safe type cast:&lt;/p&gt;
&lt;p&gt;int age = reader[&amp;quot;Age&amp;quot;] as int? ?? -1;&lt;/p&gt;
&lt;p&gt;int? age = reader[&amp;quot;Age&amp;quot;] as int?;&lt;/p&gt;
&lt;p&gt;Unfortunately, this has negative consequences that can be difficult to catch. If you mistakenly cast a smallint to byte, for example, the as operator will silently swallow InvalidCastException.&lt;/p&gt;
&lt;p&gt;I think a more reasonable solution with some measure of safety is to use a nullable cast either with or without the null coalescing operator:&lt;/p&gt;
&lt;p&gt;int age = (int?) reader[&amp;quot;Age&amp;quot;] ?? -1;&lt;/p&gt;
&lt;p&gt;int? age = (int?) reader[&amp;quot;Age&amp;quot;];&lt;/p&gt;
&lt;p&gt;The code is easy to read and it has the advantage that InvaidCastException is thrown early in case you mistakenly cast to the wrong type, which I've found is easy to do with large tables.&lt;/p&gt;
</description></item><item><title>re: DBNull and Nullable types</title><link>http://blogs.msdn.com/thottams/archive/2008/06/30/dbnull-and-nullable-types.aspx#8893443</link><pubDate>Mon, 25 Aug 2008 10:17:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8893443</guid><dc:creator>Peter Strömblad</dc:creator><description>&lt;p&gt;Thanks - the as operator with ?? &amp;lt;defaultvalue&amp;gt; works perfectly when using nullable types for the database tier.&lt;/p&gt;
</description></item><item><title>C# Null, DBNull and Entities</title><link>http://blogs.msdn.com/thottams/archive/2008/06/30/dbnull-and-nullable-types.aspx#8893631</link><pubDate>Mon, 25 Aug 2008 12:44:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8893631</guid><dc:creator>Peter Strömblad</dc:creator><description>&lt;p&gt;I've been working like mad to get my 3-tier construction to work. As I'm not all that great in C# yet,&lt;/p&gt;
</description></item><item><title>re: DBNull and Nullable types</title><link>http://blogs.msdn.com/thottams/archive/2008/06/30/dbnull-and-nullable-types.aspx#8956972</link><pubDate>Thu, 18 Sep 2008 11:49:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8956972</guid><dc:creator>Naison Garvasis Pekkattil</dc:creator><description>&lt;p&gt;Check out the SafeDataReader class from CSLA.NET. I had customized and used to solve the repeated dbnull check.&lt;/p&gt;
</description></item><item><title>re: DBNull and Nullable types</title><link>http://blogs.msdn.com/thottams/archive/2008/06/30/dbnull-and-nullable-types.aspx#8971464</link><pubDate>Wed, 01 Oct 2008 15:05:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8971464</guid><dc:creator>Chi Afriq</dc:creator><description>&lt;p&gt;How do you make these conversions using VB2005? I was initially developing my database system in C# but had prblms cz I'm a beginner, switched back to VB and the NullReferenceException and InvalidCastExceptions are still giving me hell!&lt;/p&gt;
</description></item></channel></rss>