DBNull in Datatable

Oct 29, 2010 at 3:37 PM

with this code:

var data1 = JsonConvert.DeserializeObject<DataTable>("[{Number: null},{Number: 1}]");

deserialize is OK :), data1 has 2 rows, 1 column (column's name is Number(type is string))

but,

var data2 = JsonConvert.DeserializeObject<DataTable>("[{Number: 1},{Number: null}]");

first : {Number: 1}=> data2 has 1 row and column Number is typeof ( Int64 )

second: {Number: null} => throw exception: Cannot set Column 'Number' to be null. Please use DBNull instead.

why we don't use DBNull for DataTable?

public class DataTableConverter : JsonConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{ ....
// Changed:
dr[columnName] = reader.Value ?? DBNull.Value;
// Original:
// dr[columnName] = reader.Value; } }

i'm sorry for my english :(