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

I have a page that displays two objects and then the user picks one
I have a terrible problem that hopefully has a very simple answer. I am
I have a database that consists in keeping calendars, those calendars are spread out
I have a common database joining situation involving three tables. One table, A, is
I have 4 variables: $southwest_lat, $southwest_lng, $northeast_lat, $northeast_lng I also have a database table,
I'm writing some terrible, terrible code, and I need a way to put a
A fully normalized database with no redundant data is good academically, but has terrible
Excuse the terrible question title. I have this PHP string and want to check
I have an application that I would like to have support SQL Server Mirroring.
Our application uses a large product database that is updated once a day. Updates

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.