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 8635485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:59:07+00:00 2026-06-12T09:59:07+00:00

Error Message: An object with the same key already exists in the ObjectStateManager. The

  • 0

Error Message:
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

I want to get Image Url from database if fileupload has null value(not change).
I mean if i change smallImage and not change LargeImage,then it should get largeImage value from DB.

[HttpPost]
    public ActionResult Edit(Blog blog, HttpPostedFileBase smallImage, HttpPostedFileBase largeImage)
    {
        if (ModelState.IsValid)
        {
            if (smallImage != null)
            {
                blog.SmallImage = smallImage.ContentLength + "_" + smallImage.FileName;
                string filepath = Path.Combine(Server.MapPath("~/Content/Blog/"), smallImage.ContentLength + "_" + smallImage.FileName);
                smallImage.SaveAs(filepath);
            }
            else
            {
                blog.SmallImage = db.Blogs.Find(blog.ID).SmallImage;
            }
            if (largeImage != null)
            {
                blog.LargeImage = largeImage.ContentLength + "_" + largeImage.FileName;
                string filepath = Path.Combine(Server.MapPath("~/Content/Blog/"), largeImage.ContentLength + "_" + largeImage.FileName);
                largeImage.SaveAs(filepath);
            }
            else
            {
                blog.LargeImage = db.Blogs.Find(blog.ID).LargeImage;
            }
            blog.PostDate = Convert.ToDateTime(DateTime.Now.ToShortDateString());
            db.Entry(blog).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(blog);
    }

Thank you.

  • 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-12T09:59:08+00:00Added an answer on June 12, 2026 at 9:59 am

    You are both loading a copy of the blog

     db.Blogs.Find(blog.ID)
    

    and attaching another with the same id to the context

    db.Entry(blog).State = EntityState.Modified;
    

    meaning you have 2 copies of the same blog in the context (not allowed).

    I’d recommend the replacing the one posted back with a viewmodel instead, something like

    public ActionResult Edit(BlogViewModel viewModel, HttpPostedFileBase smallImage, HttpPostedFileBase largeImage)
    {
        if (!ModelState.IsValid)
        {
            return View(viewModel);
        }
            var blog =  db.Blogs.Find(viewModel.ID);
            if (smallImage != null)
            {
                blog.SmallImage = smallImage.ContentLength + "_" + smallImage.FileName;
                string filepath = Path.Combine(Server.MapPath("~/Content/Blog/"), smallImage.ContentLength + "_" + smallImage.FileName);
                smallImage.SaveAs(filepath);
            }
    
            if (largeImage != null)
            {
                blog.LargeImage = largeImage.ContentLength + "_" + largeImage.FileName;
                string filepath = Path.Combine(Server.MapPath("~/Content/Blog/"), largeImage.ContentLength + "_" + largeImage.FileName);
                largeImage.SaveAs(filepath);
            }
    
    
            blog.Title = viewModel.Title;
            blog.Body = viewModel.Body; //etc
    
            db.SaveChanges();
            return RedirectToAction("Index");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting the following error message: Method range of object _worksheet failed when
I'm receiving this fatal error message: Using $this when not in object context. This
I am using the session object to store success/error messages based on user actions.
Error message doesn't display on mysql connect failure if( ! $this->remote_connection_id = @mysql_connect($vars['hostname'], $vars['username'],
When error message is a long string, how to make the message be displayed
The error message I gen when I try to access the web page server
My Error message: Request for member nameField in something not a struct or union
As the error message stated below, I do not use user_profiles_path as plural because
I keep getting the error message Error: 0xC020901E at Data Flow Task, Lookup ProjectId
I am getting the following error message when compiling or attempting to run my

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.