Nulls and data retrieval with .NET 2.0

  • Thread starter Thread starter Thomas Coleman
  • Start date Start date
T

Thomas Coleman

I have been playing around with 2.0 and I'm trying clarify a few things about
generics and data access. I was hoping to be able to do something like this with
nullable types:

int? foo = null;

DataRow dr = ...;
dr["Column1"] = foo;

However, this does not work if the DataType value is set to int. In that
scenario, the system throws an invalid cast exception claiming that <> cannot be
converted to Int32.

So, my question is this: in .NET 2.0, is there a way of declaring a variable
that can be nullable (either a null reference or DBNull.Value) and typed and
castable in a way that DataRow knows WTF to do with the value?

if (foo.HasValue)
dr["ColumnName"] = foo;
else
dr["ColumnName"] = DBNull.Value;

This above solution is totally unacceptable. WAY too many lines of code to write
for something that happens in thousands of lines of code. It was because of
this, among other reasons, that I had to write my own struct in .NET 1.x. I'm
hoping to be able get away from a custom struct as it makes binding and other
operations a bit more of a pain. Further, I do not want to be forced to use
SqlTypes as they are tied to SQL Server.


Thomas
 
Hi Thomas,

As far as I know, we cannot assign a null value as nullable type to a
certain column. In beta1, this option is supported. However, in beta2 and
RTM, this will not be supported. You can check the following link for more
information on this issue.

http://blogs.msdn.com/aconrad/archive/2005/02/28/381859.aspx

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
<rant>
Frankly, that sucks. So much for "eating your own dog food." .NET's lack of
support for COMMON database scenarios really leaves me stunned. The concept is
simple really: database driven systems need the ability to propogate nulls all
the way to the presentation layer. How can anyone design a database driven
system, specifically a tiered database driven system and not encounter this
problem?

What type am I supposed to use for my business objects? If I use nullable types,
I have two to four times more code I have to write when converting data from the
data tier to the business tier. If I use SqlTypes, I'm tied to SQL Server. How
in the !@#$% could this fundamental task be overlooked by the Microsoft
designers?
</rant>

So, what is NEEDED is one of the following:

1. automatic conversion of a null in a nullable type to DBNull.Value and a
DBNull.Value passed to a nullable type converted back into a null reference

2. A generic (meaning non DBMS specific) type that can handle database nulls and
mimics the primitives.

So far, 1.x, I have had to roll my own struct to handle #2. I would much prefer
something built into the framework for handling this core functionality and I am
absolutely floored that no "dog food eaters" have revolted because of this lack
of functionality.

Here's the type of thing I've tried in .NET 2.0:

DataTable dt = //db call;

int? columnValue = null;

columnValue = dt.Rows[0]["ColName"];
dt.Rows[0]["ColName"] = columnValue;

Reading through the link I see all the reasons why this shouldn't work, but as a
developer, I need a simple, one line solution for the single most fundamental
operation in all my systems: stuffing a db value into a variable. It *ought* to
be the case that this can happen without incurring a boat load of boxing calls.



Thomas
 
Hi Thomas,

As I stated this was supported in Beta1 of .NET framework 2.0 but somehow
canceled on the next version. Also it will not be available in the release.
This is the product team's design. I agree with you that this is important
to database developers. But as Bill mentioned in another thread, this might
destablize the framework. In the link I posted in my last post, you can see
it will cause a lot of incompatibilities with the DBNull in earlier
versions of frameworks.

As a supported engineer, I will pass your feedback to our product team.
Thank you for your suggestions.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top