locked
How to fill a field(with field-type Int32) in a typed datatable with DBNull.value? or leave it empty? without getting an exception? RRS feed

  • Question

  • I have a dataset with a typed datatable and a field "HomeNumber".
    (The dataset is filled with Oracle-tables)
    This number can be empty, but I cannot set the properties to accept a null value.

    Properties of this field:
    AllowDBNull : true
    DataType     :System.Int32
    DefaultValue: <DBNull>
    NullValue: (Throw exception).

    It is not possible to change the property of 'NullValue' although I don't want to get an error if this field is empty.

    How can I avoid an exception everytime this field is empty?

    I have the same problem with a field of type: DateTime.

    Thanks in advance
    • Moved by nobugz Thursday, May 28, 2009 10:00 AM not a clr q (From:Common Language Runtime)
    Thursday, May 28, 2009 9:27 AM

Answers

  • Since you cannot change the NullValue...

    For the nullable column, the typed DataSet generator makes another method:

    IsColumnNameNull()

    You need to always check this in your code before accessing the ColumnName property.

    if (row.IsColumnNameNull())
    {
      // Null handling logic here.
      Console.WriteLine("It is null.");
    }
    else
    {
      // Non-null handling logic here.
      Console.WriteLine("The value is: " + row.ColumnName);
    }
    • Marked as answer by afva Friday, May 29, 2009 9:06 AM
    Thursday, May 28, 2009 10:45 PM

All replies

  • Since you cannot change the NullValue...

    For the nullable column, the typed DataSet generator makes another method:

    IsColumnNameNull()

    You need to always check this in your code before accessing the ColumnName property.

    if (row.IsColumnNameNull())
    {
      // Null handling logic here.
      Console.WriteLine("It is null.");
    }
    else
    {
      // Non-null handling logic here.
      Console.WriteLine("The value is: " + row.ColumnName);
    }
    • Marked as answer by afva Friday, May 29, 2009 9:06 AM
    Thursday, May 28, 2009 10:45 PM
  • Thank you so very much.
    I have spend hours looking for something like this!
    Friday, May 29, 2009 9:06 AM