why DataColumn AllowDbNull is true even if oracle db does not allow null

Posted by matti on Stack Overflow See other posts from Stack Overflow or by matti
Published on 2010-04-17T15:02:44Z Indexed on 2010/04/17 16:53 UTC
Read the original article Hit count: 301

Filed under:
|
|
|

Hi. I have column SomeId in table SomeLink. When I look with tOra or Sql Plus Worksheet both state:

tOra:

Column name     Data type   Default   Null      Comment
SOMEID          INTEGER     {null}    NOT NULL   {null}

Sql Plus:

SOMEID      NOT NULL NUMBER(38)

I have authored a method that's intended to give default values to all NOT NULL fields that don't have values:

public static void GetDefaultValuesForNonNullColumns(DataRow row)
{
    foreach(DataColumn col in row.Table.Columns)
    {
        if (Convert.IsDBNull(row[col]) && !col.AllowDBNull)
        {
            if (ColumnIsNumeric(col.DataType))
                row[col] = 0;
            else if (col.DataType == typeof(DateTime))
                row[col] = DateTime.Now;
            else if (col.DataType == typeof(String))
                row[col] = string.Empty;
            else if (col.DataType == typeof(Char))
                row[col] = ' ';
            else 
                throw new Exception(string.Format("Unsupported column type: {0}", col.DataType));
        }
    }   
}

When SOMEID is handled in loop the AllowDBNull = true.

I really can't understand. The table is created in DataSet like this:

_someLinkAdptr = _dbFactory.CreateDataAdapter();
_someLinkAdptr.SelectCommand = _dbFactory.CreateCommand();
_someLinkAdptr.SelectCommand.Connection = _cnctn;
_someLinkAdptr.SelectCommand.CommandText = GetSomeLinkSelectTxtAndParams(_someLinkAdptr.SelectCommand, 
                            UndefinedValue.ToString(), UndefinedValue.ToString());

Select command returns no rows. The idea is that I can then use commandbuilder to get InsertCommand without building it myself. The row is added to dataset's table like this:

private static void CreateDocLink(int anId, int anotherId)
{
    DataRow row = _someDataSet.Tables["SomeLink"].NewRow();

    row["AnId"] = anId;
    row["AnotherId"] = anotherId;
    Utility.GetDefaultValuesForNonNullColumns(row);

    _someDataSet.Tables["SomeLink"].Rows.Add(row);
}

When DataAdapter is updated to oracle db I get:

ORA-01400: cannot insert NULL into (SOMESCHEMA.SOMELINK.SOMEID)

Cheers & BR -Matti

© Stack Overflow or respective owner

Related posts about c#

Related posts about dbnull