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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:37:18+00:00 2026-05-29T12:37:18+00:00

we have a .NET 3.5 WinForms app that uses Linq2Sql to insert records into

  • 0

we have a .NET 3.5 WinForms app that uses Linq2Sql to insert records into SQL Server 2008 database. It worked fine for the past couple of years until yesterday we noticed that a submitchanges() call lead to multiple insertions of the records. Instead of inserting each new record just once (as it always does and should do), the query ran five times in total and so each record ended up being inserted 5 times!!!
This of course is a huge problem, we cannot have duplicate records in our database appearing randomly.
We have no idea why this happenned, I am suspecting some network issue. Does anyone have a clue what could be the reason, how to troubleshoot it and how to get rid of it?

edit: here’s a code sample:

if (selectedOrders > 0)  
{  
    try  
    {  
        foreach (Customer cust in selectedOrders.Select(a=>a.Customer))  
        {  
            Invoice newInvoice = new Invoice();  
            newInvoice = cust.CustomerID;  
            // ... other code here  
            db.Invoices.InsertOnSubmit(newInvoice);  
            db.SubmitChanges();  
        } 
    }  
    catch (Exception ex)  
    {  
        log.Items.Add("Error: " + ex.Message);  
        log.SelectedIndex = log.Items.Count - 1;  
        log.Update();  
    }
}

Thanks.
Jan

  • 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-29T12:37:19+00:00Added an answer on May 29, 2026 at 12:37 pm

    You should use Distinct() method after selecting customers because, there may be same customer in selected orders. Also when inserting multiple records to one table, it is better to use db.SubmitChanges() outside the foreach loop.

    var customers = selectedOrders.Select(a=>a.Customer).Distinct();
    foreach (Customer cust in customers)  
    {  
        Invoice newInvoice = new Invoice();  
        newInvoice = cust.CustomerID;  
        // ... other code here  
        db.Invoices.InsertOnSubmit(newInvoice);                  
    } 
    db.SubmitChanges(); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a .NET 2.0 winforms app that connects to a SQL Server 2005
We have a huge client/server WinForms app that uses .NET remoting to pass DAOs
I have a WinForms app that uses the Settings feature of .NET 2, but
I have a .NET 4 WinForms app that uses the ADO.NET Entity Framework. Some
We have an MSI installer for a .Net WinForms app for Windows XP that
I have created a setup project in VS2008. My WinForms app uses .NET 2.0,
Suppose I have a WinForms app that is built and compiled against .NET 2.0
I have a .NET Winforms app (created in VS2005) that I deploy using ClickOnce.
I have a C# .NET 2.0 WinForms app. My app has a control that
I have a Winforms App (.NET 3.X) That runs a method in a class

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.