如何在Integer类型的数据表的列中设置空值

时间:2021-07-03 15:29:20

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.

我在数据表中有一个整数列,我想在该列中设置空值。我已经将AllowDBNull设为true,但如果添加了null值,它仍然无效。

Following are the configuration which I have done for Column

以下是我为Column所做的配置

如何在Integer类型的数据表的列中设置空值

I am getting following exception 如何在Integer类型的数据表的列中设置空值

我得到以下异常

Can anyone help mi for this?

任何人都可以帮助mi吗?

5 个解决方案

#1


2  

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()

您无法将DBNull分配给DataTable中的字段。在DataTable中允许DBNull时,会生成一个额外的方法。在这种情况下: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()

这也是在检查值时。你不能直接使用'property'ParentApplicationRoleId,如果你想读它,你将首先使用另一个生成的方法检查它: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");
  }
}

#2


0  

The "?" behind a datatype makes it nullable.

“?”在数据类型后面使它可以为空。

Example:

int? t = null; 

So nzrytmn is right!

所以nzrytmn是对的!

#3


0  

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

您可以尝试将数据列的NullValue属性设置为不是“(ThrowException)”的内容

#4


0  

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

您是否尝试将DataType设置为Sytem.Int32?可能它必须是可空的。

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

你能检查一下这个答案表是可以为空的DateTime,但是DataSet会抛出异常吗?

#5


-2  

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

请将该值声明为nullable int(int?)。然后它将接受所有空值

#1


2  

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()

您无法将DBNull分配给DataTable中的字段。在DataTable中允许DBNull时,会生成一个额外的方法。在这种情况下: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()

这也是在检查值时。你不能直接使用'property'ParentApplicationRoleId,如果你想读它,你将首先使用另一个生成的方法检查它: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");
  }
}

#2


0  

The "?" behind a datatype makes it nullable.

“?”在数据类型后面使它可以为空。

Example:

int? t = null; 

So nzrytmn is right!

所以nzrytmn是对的!

#3


0  

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

您可以尝试将数据列的NullValue属性设置为不是“(ThrowException)”的内容

#4


0  

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

您是否尝试将DataType设置为Sytem.Int32?可能它必须是可空的。

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

你能检查一下这个答案表是可以为空的DateTime,但是DataSet会抛出异常吗?

#5


-2  

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

请将该值声明为nullable int(int?)。然后它将接受所有空值