Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Null Datacolumns

Status
Not open for further replies.

chronics

Programmer
Sep 10, 2006
35
GB
Hi

Can anyone help explain to me why I get an exception when i run the following code.

I have a datatable that has a number of columns of type int. When I add in a new row I can pass in a value of null to these rows and no exception is generated. When I proceed to iterate through the rows if I if I input a value of null I get an:

Argument Exception - Cannot set column to be null, please use DBNull instead.

If I attempt to make the columns of type int? I get a: NotSupportedException - Dataset does not support System.Nullable<>

Why do I get these exceptions, using a value of DBNull.value would not be difficult although it would mean I'd have to write more code, however I would like to understand what is actually going on here.

Thanks
 
null is null
DbNull.Value is not null it's an object of type DbNull.

int is not a nullable type. the default value of int is 0. (which is contrary to types like string and all classes whose default value is null)

int? is syntax sugar for new Nullable<int>(); which allows for null numbers, but I don't think datatables can use the nullable<> class.

also be aware that data columns can be configured to either ignore, or throw exceptions with DbNull values.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks for the quick reply JMeckley but could you help me explain the following code


int? myint;
if(condition is true)
{
myint = 5;
}
else
{
myint = null;
}
myDataTable.Rows.Add(2, myint);

No Exception is raised when the above code is run even when myint has a null value. However an exception is raised when the following is subsequently run:

foreach(DataRow dr in myDataTable.Rows)
{
dr[1] = myint;
}

In the debugger after the row is originally added the column has a value of {}. Is this null or DBNull.Value. I dont understand whats going on behind the scenes here.
 
i'm not sure. I haven't worked with datatables, other than dumb data containers for reports, for a long time. When working with reports I use simple datatypes and defaults values, I don't use null values with datatables.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks for the quick reply JMeckley. Having done a quick test I can confirm the behaviour above. It appears as though the compiler at some point converts null values to DBNull values when a new row is being added but expects an explicit DBNull value when a value within a row is being changed.

Thanks once again. Happy easter.
 
Yup. Null != DBNull, for some occasional values of null.

What's happening is the compiler is doing a type promotion -- when it knows it can safely do this. Since you know more about what your program ought to be doing than the compiler, you need to explicitly do those conversions.

Chip H.



____________________________________________________________________
www.chipholland.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top