KopBuH
KopBuH

Reputation: 140

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

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?

Upvotes: 0

Views: 658

Answers (2)

Nitin Joshi
Nitin Joshi

Reputation: 1666

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.

Upvotes: 0

Imad S.
Imad S.

Reputation: 375

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.

Upvotes: 0

Related Questions