How do I force a record to save itself?
Posted: Sun Feb 22, 2009 10:58 am
Please bear in mind that I don't have access to the DAL code.
I created a class, ModifiedProductVariant, derived from ProductVariant. In my class, I define a custom field that I want saved when I call my overridden Save() method. This works as long as the ProductVariant record has already been created because a ProductVariantID already exists. But if it's a new record, I will need a corresponding ProductVariantID which doesn't exist until the record is saved. If my field is changed and the standard ProductVariant fields are not, my field isn't stored because the DAL doesn't detect that the record is dirty. Marking the record as dirty via setting IsDirty has no effect.
I also tried to fool the DAL by faking a change to one of the standard ProductVariant fields by writing a bogus value to one of the standard fields but it doesn't work either. Is there a way to set the record dirty so that my field gets stored?
Here's my code for reference:
public override SaveResult Save()
{
if (DataSource == null)
throw new Exception("VariantUOM not initialized");
string oldValue = this.Sku; // fool DAL into thinking the record is dirty (IsDirty doesn't work)
this.Sku = "FakeChange";
SaveResult result = base.Save();
DataSource.ReadVariants(); // Record saved, reread dataset. Record now has a ProductVariantID
DataSource.SetOptionChoice(this.ProductVariantId, this.Uom); // Set the value of our field in the dataset
DataSource.WriteVariant(); // issue an update, value our field is stored
this.Sku = oldValue; // restore the original value
result = base.Save();
return result;
}
I created a class, ModifiedProductVariant, derived from ProductVariant. In my class, I define a custom field that I want saved when I call my overridden Save() method. This works as long as the ProductVariant record has already been created because a ProductVariantID already exists. But if it's a new record, I will need a corresponding ProductVariantID which doesn't exist until the record is saved. If my field is changed and the standard ProductVariant fields are not, my field isn't stored because the DAL doesn't detect that the record is dirty. Marking the record as dirty via setting IsDirty has no effect.
I also tried to fool the DAL by faking a change to one of the standard ProductVariant fields by writing a bogus value to one of the standard fields but it doesn't work either. Is there a way to set the record dirty so that my field gets stored?
Here's my code for reference:
public override SaveResult Save()
{
if (DataSource == null)
throw new Exception("VariantUOM not initialized");
string oldValue = this.Sku; // fool DAL into thinking the record is dirty (IsDirty doesn't work)
this.Sku = "FakeChange";
SaveResult result = base.Save();
DataSource.ReadVariants(); // Record saved, reread dataset. Record now has a ProductVariantID
DataSource.SetOptionChoice(this.ProductVariantId, this.Uom); // Set the value of our field in the dataset
DataSource.WriteVariant(); // issue an update, value our field is stored
this.Sku = oldValue; // restore the original value
result = base.Save();
return result;
}