How can I include DBNull as a value in my strongly typed dataset?

Posted by Beska on Stack Overflow See other posts from Stack Overflow or by Beska
Published on 2009-09-22T18:01:24Z Indexed on 2010/05/31 11:32 UTC
Read the original article Hit count: 203

I've created a strongly typed dataset (MyDataSet) in my .NET app. For the sake of simplicity, we'll say it has one DataTable (MyDataTable), with one column (MyCol). MyCol has its DataType property set to "System.Int32", and its AllowDBNull property set to "true".

I'd like to manually create a new row, and add it to this dataset. I create the row without a problem, with something like:

MyDataSet.MyDataTableRow myRow = MySimpleDataSet.MyDataTable.NewItemRow();

Fine. However, when I try to set the value to DBNull:

myRow.MyCol = DBNull.Value;

I'm told that I can't do it...that it can't cast that to an int. This makes sense, in a way, since I've defined it to be an int...but then how can I get DBNull in there? Am I not supposed to be able to have DBNull in there? Isn't that what the AllowDBNull property is for?

I'm obviously missing something fundemental. Can someone help explain what it is?

EDIT: I also tried entering "int?" as the DataType, but Visual Studio throws an error when I enter it, saying that "Column requires a valid DataType."

© Stack Overflow or respective owner

Related posts about .NET

Related posts about strongly-typed-dataset