Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8771137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:42:41+00:00 2026-06-13T17:42:41+00:00

I have a small demo application to work around Binary data stored in the

  • 0

I have a small demo application to work around Binary data stored in the database. See below for related code:

Entity Class:

    [HiddenInput(DisplayValue = false)]
    public int Id { get; set; }

    [Display(Name = "Full Name:")]
    public string Name { get; set; } 

    [DataType(DataType.Upload)]
    [Display(Name = "Photo:")]
    public byte[] ImageData { get; set; }

    [HiddenInput(DisplayValue = false)]
    public string ImageMimeType { get; set; }

Edit Action:

   [HttpGet]
   public ActionResult Edit(int id)
    {
         var mensPlayer = _dataSource.MensPlayers.FirstOrDefault(p => p.Id == id);

        return View(mensPlayer);
    }

    [HttpPost]
    public ActionResult Edit(MensPlayer mensPlayer, HttpPostedFileBase image)
    {
        if (ModelState.IsValid)
        {
           //Added line below
            _dataSource.ImageTemp(mensPlayerInDb, mensPlayer);
            if (image != null)
            {
                mensPlayer.ImageMimeType = image.ContentType;
                mensPlayer.ImageData = new byte[image.ContentLength];
                image.InputStream.Read(mensPlayer.ImageData, 0, image.ContentLength);
            }
              //Added line below
             mensPlayer.ImageData = mensPlayerInDb.ImageData;

            //Save Player
            _dataSource.Update(mensPlayer);
           _dataSource.Save();
          TempData["message"] = string.Format("{0} has been saved", mensPlayer.Name);
            return RedirectToAction("Detail", "MensPlayer", new {id = mensPlayer.Id});
        }
        return View(mensPlayer);
    }

GetImage Method:

public FileContentResult GetImage(int id)
    {
        var image = _dataSource.MensPlayers.FirstOrDefault(p => p.Id == id);
        if (image != null)
        {
            return File(image.ImageData, image.ImageMimeType);
        }
        return null;
    }

Display Image:

@if (Model.ImageData != null) {
    <div >
         <img width="300" height="400" src="@Url.Action("GetImage", "MensPlayer",
                                        new { Model.Id })" alt="Player Image"/>
     </div>
 }

DataSource class

public interface IDataSource
{
    IQueryable<MensTeam> MensTeams { get; }
    IQueryable<MensPlayer> MensPlayers { get; }
    IQueryable<MensHome> MensHomes { get; }
    void Save();
    void Update(MensPlayer mensPlayer);
    void Delete();
    void ImageTemp(MensPlayer mensPlayerInDb, MensPlayer mensPlayer);//I added this
}

In Db class

void IDataSource.Save()
    {
        SaveChanges();
    }

    void IDataSource.Update(MensPlayer mensPlayer)
    {
        Entry(mensPlayer).State = EntityState.Modified;
    }
     //Added code below
    void IDataSource.ImageTemp(MensPlayer mensPlayerInDb, MensPlayer mensPlayer)
    {
        Entry(mensPlayerInDb).CurrentValues.SetValues(mensPlayer);
    }

My problem is, every time i try to edit a player, every data is nicely retrieved from the database but when i hit save after editing, the ImageData is lost, more like replacing the original data with null value. It’s like, the application expects me to re-upload the image at every edit i attempt.

What can i do to clean this up?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:42:42+00:00Added an answer on June 13, 2026 at 5:42 pm

    Well, if image is null then in your entity mensPlayer.ImageData is null. Later when you call _dataSource.Update(mensPlayer) you set the state of the entity to Modified (at least I guess that Update just sets the state to Modified), so you are telling EF that your have modified the image too (namely set it to null) and EF will save that change.

    In order to solve the problem you can either load the entity including the image from the database first…

    var mensPlayerInDb = context.MensPlayers.Find(mensPlayer.Id);
    mensPlayer.ImageData = mensPlayerInDb.ImageData; // save the ImageData
    // copy changed values to loaded entity and save it, ImageData is unchanged
    context.Entry(mensPlayerInDb).CurrentValues.SetValues(mensPlayer);
    context.SaveChanges();
    

    …or you can try:

    context.Entry(mensPlayer).State = EntityState.Modified;
    context.Entry(mensPlayer).Property(m => m.ImageData).IsModified = false;
    

    The latter approach didn’t work with EF < 5.0 (because it was forbidden to set the Modified state of a property to false once the entity was marked as Modified) but it should work with EF 5.

    You need to integrate one of the solutions into your _dataSource service class, possibly by introducing new methods because it won’t work with your general Update method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a small demo application in Java using Spring, that needs to have
I have prepared an application that is a small demo of Student information manipulation.
I have small query today I happen to see when I typed a website
I have small question by entity framework 4.1. There is interface of image DTO:
I have small question. When I started to programm my application, I grouped types
I have small database of business and their addresses. Using the Google Geocode API
I'm studying Prism and need to create a small demo app. I have some
i have been working on a small demo and i wrote a function which
I have a small project which stores data in IndexedDB in browser. I would
I have written a small Android Demo to use TTS in different languages. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.