Domanda

I am using FormView control, and I want to programmatically insert null value in OnItemInserting and OnItemUpdating event, by NewValues dictioanry in FormViewUpdateEventArgs parameter. But if I use

protected void FormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
    e.NewValues["EntityFrameworkEntityPropertyName"] = null;
}

such modification is ignored, database column is not updated and keeps its old value. EntityFramework entity property is: Type Int32, Nullable True, StoreGeneratedPattern None.

If I bind TextBox control directly to property by Bind(), nulls are inserted well. Also if values are different from null, everything works fine.

How can I insert null value?

È stato utile?

Soluzione 2

Ok, I found solution. DetailsView and FormView in OnItemUpdating event compares previous and new values and if these are changed, update it. When I put null into e.NewValues, while my TextBox is not bounded by Bound function, e.OldValues["EntityFrameworkEntityPropertyName"] doesn't exists (means null) and in NewValues is null also, so column is not updated.

I simply set e.OldValues["EntityFrameworkEntityPropertyName"] to anything (but same type) and now inserting nulls works fine.

protected void FormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
    e.OldValues["EntityFrameworkEntityPropertyName"] = -1
    e.NewValues["EntityFrameworkEntityPropertyName"] = null;
}

My textbox is not bounded directly, because I need to process its value before I will show it up to user. On insert/update I need also some value processing.

Altri suggerimenti

Try something like

  e.NewValues["EntityFrameworkEntityPropertyName"] = System.DBNull.Value;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top