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

  • SEARCH
  • Home
  • 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 8235731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:54:38+00:00 2026-06-07T18:54:38+00:00

Currently I am attempting to add a new row to a database table through

  • 0

Currently I am attempting to add a new row to a database table through AJAX which is working fine. But then I try to update a different table and I get an error. Here is my code and the error I am encountering.

Error

The object cannot be attached because it is already in the object context. An object can only be reattached when it is in an unchanged state.

Line 41: _db.ChampionCounters.Attach(champion);

Code

[HttpPost]
    public ActionResult VoteYes(int id)
    {
        string results;

        if (Request.IsAuthenticated)
        {
            var checkFirst =
                from c in _db.UserCounterLinks
                where c.counterId == id && c.userName == User.Identity.Name
                select c;

            if (checkFirst.Any())
            {
                results = "You have already voted on this counter.";
                return Json(results);
            }

            var userVoteLink = new UserCounterLink { counterId = id, userName = User.Identity.Name, userAgree = true };

            _db.UserCounterLinks.AddObject(userVoteLink);


            var champion = _db.ChampionCounters.SingleOrDefault(c => c.id == id);

            if (champion != null)
            {
                champion.positiveVotes++;
                _db.ChampionCounters.Attach(champion);
            }
            _db.SaveChanges();
            results = "Voted";
        } else
        {
            results = "You must be logged in to vote.";
        }

        return Json(results);
    }

Summary

The code above is from the controller that handles the Ajax post. Like I said the userVoteLink table creates a record just fine. But when I try to update the other table ChampionCounters the error is thrown.

Thanks in advance!

  • 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-07T18:54:40+00:00Added an answer on June 7, 2026 at 6:54 pm

    You don’t need to attach the instance because the context is already tracking that instance. Just remove the _db.ChampionCounters.Attach(champion); line.

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

Sidebar

Related Questions

I'm currently working with the AJAX:UpdatePanelAnimationExtender and I've implemented it in code behind which
I'm currently attempting to retrieve a list of objects from my database using jQuery.
I am attempting to add BASIC authentication to my RESTful web-service. Currently I have
Currently, working with RC0 Denali, having some issues I am attempting to review data
I'm attempting to open an existing pdf file and then add another page to
The following is my code for attempting to add create a JButton which will
I am currently attempting to implement a custom gridview interface to display data from
I'm currently attempting to migrate a legacy VBA/Microsoft Access application to Python and PyQt.
I am currently attempting to use Lucene to search data populated in an index.
I'm currently attempting to code one for part of a college project - binary/hex

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.