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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:10:26+00:00 2026-05-22T18:10:26+00:00

I am terrible with databases so please bear with me. I have a program

  • 0

I am terrible with databases so please bear with me.

I have a program that gets some user information and adds it to a database table. I then later need to get more information for that table, and update it. To do so I have tried doing this:

    public static void updateInfo(string ID, string email, bool pub)
    {
        try
        {
            //Get new data context
            MyDataDataContext db = GetNewDataContext(); //Creates a new data context

            //Table used to get user information
            User user = db.Users.SingleOrDefault(x => x.UserId == long.Parse(ID));

            //Checks to see if we have a match
            if (user != null)
            {
                //Add values
                user.Email = email;
                user.Publish = publish;
            }

            //Prep to submit changes
            db.Users.InsertOnSubmit(user);
            //Submit changes
            db.SubmitChanges();
        }
        catch (Exception ex)
        {
            //Log error
            Log(ex.ToString());
        }
    }

But I get this error:

System.InvalidOperationException: Cannot add an entity that already exists.

I know this is because I already have an entry in the table, but I don’t know how to edit the code to update, and not try to make a new one?

Why does this not work? Wouldn’t submitting changes on a current item update that item and not make a new one?

  • 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-05-22T18:10:27+00:00Added an answer on May 22, 2026 at 6:10 pm

    The problem is

    //Prep to submit changes
    db.Users.InsertOnSubmit(user);
    

    Because you got the user from the DB already, you don’t need to re-associate it with the context.

    Comment that out and you’re good to go.

    Just a style / usage comment as well. You should dispose your context:

    public static void updateInfo(string ID, string email, bool pub)
    {
        try
        {
            using (MyDataDataContext db = GetNewDataContext()) 
            {
                User user = db.Users.SingleOrDefault(x => x.UserId == long.Parse(ID));
    
                if (user != null)
                {
                    user.Email = email;
                    user.Publish = publish;
                }
    
                db.SubmitChanges();
            }
        }
        catch (Exception ex)
        {
            //Log error
            Log(ex.ToString());
    
            // TODO: Consider adding throw or telling the user of the error.
            // throw;
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I make relationsships with JPA using hibernate, some terrible long and ackward column
I've a terrible memory. Whenever I do a CONNECT BY query in Oracle -
I'm terrible with regex, but I've had a try and a Google (and even
Ugh ok I'm terrible at explaining things, so I'll just give you the quotes
I'm terribly new to SQL, and cannot seem to get the desired information out,
I have an app with a large ListView which is terribly slow so I'm
I'm terribly tired of checking all my arguments for null, and throwing ArgumenutNullExceptions when
Sorry, I'm not terribly experienced with Ant. I like the eclipse Export ant buildfile
Okay, so this probably sounds terribly nefarious, but I need such capabilities for 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.