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
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