ASP.NET LinqDataSource "OnUpdating" method not update value to null

651 Views Asked by At

I have asp.net page with a linqdatasource, it use the OnUpdating method.

OnUpdating="Entity_OnUpdating"

In code-behind:

protected void Entity_OnUpdating(object sender, LinqDataSourceUpdateEventArgs e)
{
    MyClass objEntity = (MyClass)e.NewObject;
    if (expression)
        MyClass.InfoID = Guid.NewGuid();
    else
        MyClass.InfoID = null;
}

If the expression is false, and MyClass.InfoID was not null before update, MyClass.InfoID not changes to null.

UPD: MyClass has two fields: 1) ID - primary key, 2) InfoID - nullable foreign key.

Do you know, why it work like this?

2

There are 2 best solutions below

0
Imad S. On

What does your MyClass object looks like? Without looking at the object, I can guess that the property 'ID' is either listed as Primary key, Foreign Key, or simply does not allow nulls.

0
Nitin Joshi On

I think type of InfoID property is Guid which is not nullable, so when you are trying to assign a null value to it, it is not accepting.

Change the type of InfoID property to Nullable<Guid> and it would work.