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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:43:27+00:00 2026-06-01T13:43:27+00:00

Here is my code after modifications, things seems working fine but I am receiving

  • 0

Here is my code after modifications, things seems working fine but I am receiving an error on _db.SubmitChanges();

Error is

Cannot add an entity with a key that is already in use.

Code:

foreach (tblCourseNeededHours record in thisTable)
{
            tblCourseNeededHours newCNHR = new tblCourseNeededHours();
            newCNHR.Semester = a_semesterToOrganize;
            newCNHR.AssignToInstituteAdministrator = false;
            newCNHR.Freezed = false;

            _db.tblCourseNeededHours.InsertOnSubmit(newCNHR);
}

// submit all the changes
_db.SubmitChanges();

I am using MVC 2.0 and SQL Server. I have a table called tblCourses.

I want to select rows based on some selection criteria, then I want to append these rows to tblCourse.

Do I need to create a temporary table tmpCourse and to fill in these selected rows and then append them those back to tblCourse? Or can I do it without temporary table?

Any suggestion, post link ?

  • 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-01T13:43:29+00:00Added an answer on June 1, 2026 at 1:43 pm

    I believe you can just do something like:

    INSERT INTO dbo.tblCourse(list of columns)
        SELECT (list of columns)
        FROM dbo.tblCourse
        WHERE (your condition here.....)
    

    Of course, the list of columns must match, e.g. you have to have the same number of columns, and the same datatypes. Also, you cannot insert values into e.g. IDENTITY or computed columns.

    Update: to do this in Linq-to-SQL, you’d have to have an entity that can represent your data in some way. Then:

    • select the data from your existing database into a List<Entity> (or whatever your entity is really called)
    • create new objects (entities) based on those retrieved – change your properties as needed
    • insert those entities back into the data context
    • save the changes.

    Something along the lines of this code snippet (here I have a table countries which has an ISOCode and a CountryName for some countries; I’m selecting a few, and creating new ones based on those retrieved, adding those new ones to the Linq-to-SQL DataContext and saving in the end):

    // create and use the Linq-to-SQL DataContext
    using (LinqSampleDataContext ctx = new LinqSampleDataContext())
    {
        // select some data
        IQueryable<country> existingCountries = ctx.countries.Where(c => c.CountryID < 100);
    
        // loop over selected data - create new entities based on data retrieved
        foreach (country c in existingCountries)
        {
            country newCountry = new country();
            newCountry.CountryName = c.CountryName;
            newCountry.ISOCode = "X" + c.ISOCode.Substring(1);
    
            // add new entities to DataContext
            ctx.countries.InsertOnSubmit(newCountry);
        }
    
        // submit all the changes
        ctx.SubmitChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the code I use to load a texture. image is a CGImageRef. After
Here is an Example of the app. The essential code is in: golang-code/handler/handler.go (After
It is Hallowe'en after all. Here's the problem: I'm maintaining some old-ish J2EE code,
Ok the error is showing up somewhere in this here code if($error==false) { $query
Here is code from MSDN . I don't understand why the work isn't just
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
here is code for regular expression matching #include<iostream> #include<stdio.h> #include<string.h> using namespace std; int
Here is code example class A{ int i; public: A(int i) : i(i) {}
enter code here I have a table on SQL server 2005 with bigint primary
I have made some modifications to nsMediaStream.h/cpp in the Mozilla (6.0.2) code and one

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.