Mohini Jamdade
Mohini Jamdade

Reputation: 488

How to set null value in column of data table of type Integer

I am having one integer column in a data table and I want to set null value in that column. I have done AllowDBNull true but still it doesn't work if a null value is getting added.

Following are the configuration which I have done for Column

enter image description here

I am getting following exception enter image description here

Can anyone help mi for this?

Upvotes: 2

Views: 4172

Answers (5)

nzrytmn
nzrytmn

Reputation: 6941

Did you try set DataType as Sytem.Int32? May it have to be nullable.

Could you pls check this answer Table is nullable DateTime, but DataSet throws an exception?

Upvotes: 0

Jeroen van Langen
Jeroen van Langen

Reputation: 22038

You can't assign DBNull to a field in the DataTable. When you allow DBNull in a DataTable, an extra method is generated. In this case: SetParentApplicationRoleIdNull()

for example:

var newRow = DataSet.ChildApplications.NewChildApplicationRow();
newRow.AField = "Some text";
newRow.SetParentApplicationRoleIdNull();
DataSet.ChildApplications.AddChildApplicationRow(newRow);

This is also while checking the value. You can't directly use the 'property' ParentApplicationRoleId, If you want to read it, you'll have the check it first with another generated method: IsParentApplicationRoleIdNull()

for example:

foreach(var row in DataSet.ChildApplications.Rows)
{
    if(!row.IsParentApplicationRoleIdNull())
    {
        Trace.WriteLine($"ParentApplicationRoleId: {row.ParentApplicationRoleId}");
    }
    else
    {
        Trace.WriteLine("ParentApplicationRoleId: is DBNull, and can't be shown");
  }
}

Upvotes: 3

Manuel Hoffmann
Manuel Hoffmann

Reputation: 547

You can try setting the NullValue property of the data column to something that is not "(ThrowException)"

Upvotes: 0

ysfaran
ysfaran

Reputation: 6962

The "?" behind a datatype makes it nullable.

Example:

int? t = null; 

So nzrytmn is right!

Upvotes: 0

Basith khan
Basith khan

Reputation: 1

Please declare that value as nullable int(int?). then it will accet all null values

Upvotes: -2

Related Questions