ASP.NET, Linq2SQL, and Conditional Updates RRS feed

  • Question

  • Hi Linq Team,
    I'm not sure if this should be filed under ASP.NET or Linq2SQL.  If this is the wrong place, please let me know where to post this question.

    Anyway, Here's how my app is set up:
    I have a Linq2SQL table named "Product".  This table has a field named "ProductImage" (nullable byte[] w/ delay loaded) which is a varbinary(MAX) NULL.

    In my ASP.NET Page I have a FormView > LinqDataSource > DataContext which is configured to talk to my Products table.

    In the FormView's insert page, I have a FileUploader which I've added some code to handle grabbing file content on insert.  Here's the code:

    Code Snippet

    protected void fvProduct_ItemInserting(object sender, FormViewInsertEventArgs e) {
    var Uploader = (FileUpload)fvProduct.FindControl("UploadProductImageInsert");
    if (Uploader.FileBytes.Length > 0) {
    e.Values["ProductImage"] = Uploader.FileBytes;

    This handles inserting the product image just fine.

    For my edit screen, what I want to have happen is if the user specifies a new ProductImage, then it will get updated with the new value, otherwise, I do not want the product image updated. However, when I use this code:

    Code Snippet

    protected void fvProduct_ItemUpdating(object sender, FormViewUpdateEventArgs e) {
    var Uploader = (FileUpload)fvProduct.FindControl("UploadProductImageEdit");
    if (Uploader.FileBytes.Length > 0) {
    e.NewValues["ProductImage"] = Uploader.FileBytes;

    if the user does not specify a new file, the old one gets overwritten with NULL. I took a look in the LinkqDataSource_ItemUpdating event and it looks like I need to set the "e.NewObject._ProductImage.HasValue" field to False, however, I am not able to do that as it is a private field.

    Do you have any suggestions on how I can keep my image from getting overwritten if a NULL is supplied for it?

    Thanks a ton!
    -Tony V.
    Saturday, June 28, 2008 7:45 PM


All replies