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

The Archive Base Latest Questions

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

Morning, I have an issue with some of my code… Basically i am trying

  • 0

Morning,

I have an issue with some of my code…
Basically i am trying to update or insert into the database. the first if statement if for when adding a new product. the else should then update any existing products.

However when i run it, it is not updating the existing products in the database. It is however setting the items ready to be updated. Any ideas?

Many thanks…

using (aboDataDataContext dc = new aboDataDataContext())
            {
                foreach (abcProduct p in abcProducts)
                {

                    var match = (from t in dc.abcProducts
                                 where t.sku == p.productcode
                                 select t).FirstOrDefault();

                    if (match == null)
                    {

                        // Watch out here; there is some type conversion required for certain fields!
                        abcProduct prod = new abcProduct();

                        prod.sku = p.productcode;
                        prod.categoryId = dc.Categories.Single(c => c.Name == p.category).Id;
                        prod.title = p.name;
                        prod.brand = p.manufacturer;
                        prod.description = p.description;
                        prod.abcPrice = p.price;
                        prod.size = decimal.TryParse(p.size.Replace("cl", ""), out size) == true ? (int?)size : null;
                        prod.country = p.country;
                        prod.region = p.region;
                        prod.vintage = int.TryParse(p.vintage, out vintage) == true ? (int?)vintage : null;
                        prod.weight = Convert.ToDecimal("1.50");
                        prod.strength = p.strength;
                        prod.bottler = p.bottler;
                        prod.age = int.TryParse(p.age, out age) == true ? (int?)age : null;
                        prod.caskType = p.casktype;
                        prod.series = p.series;
                        prod.flavour = p.flavour;
                        if (p.freestock <= 0) { prod.stock = 0; } //check to see if stock is 0
                            else { prod.stock = p.freestock; }
                        prod.abcUpdated = false;
                        prod.stockUpdated = false;
                        prod.priceUpdated = false;
                        prod.pricePublished = false;
                        prod.stockPublished = false;
                        prod.imgPublished = false;
                        prod.prodPublished = false;
                        prod.lastUpdated = DateTime.Now;

                        // Add the new object to the abcProducts table (only in memory here)
                        dc.abcProducts.InsertOnSubmit(prod);
                    }
                    else
                    {
                        // update row
                        match.abcUpdated = true;
                        //Set if an item has been updated or not.
                        if (match.stock == p.freestock) { match.stockUpdated = false; }
                        else { match.stockUpdated = true; }
                        if (match.abcPrice == p.price) { match.priceUpdated = false; }
                        else { match.priceUpdated = true;}
                        match.sku = p.productcode;
                        match.categoryId = dc.Categories.Single(c => c.Name == p.category).Id;
                        match.title = p.name;
                        match.brand = p.manufacturer;
                        match.description = p.description;
                        match.stock = p.freestock;
                        match.abcPrice = p.price;
                        match.size = decimal.TryParse(p.size.Replace("cl", ""), out size) == true ? (int?)size : null;
                        match.weight = Convert.ToDecimal("1.50");
                        match.country = p.country;
                        match.region = p.region;
                        match.vintage = int.TryParse(p.vintage, out vintage) == true ? (int?)vintage : null;
                        match.strength = p.strength;
                        match.bottler = p.bottler;
                        match.age = int.TryParse(p.age, out age) == true ? (int?)age : null;
                        match.caskType = p.casktype;
                        match.series = p.series;
                        match.flavour = p.flavour;
                        if (p.freestock <= 0) { match.stock = 0; } //check to see if stock is 0
                            else { match.stock = p.freestock; }
                        match.abcUpdated = true;
                        match.pricePublished = false;
                        match.stockPublished = false;
                        match.imgPublished = false;
                        match.prodPublished = false;
                        match.lastUpdated = DateTime.Now;
                    }
                }

                // Finally, request Linq to perform the database updates.
                dc.SubmitChanges();
            }
            return null;
        }
  • 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-07T01:54:22+00:00Added an answer on June 7, 2026 at 1:54 am

    There is problem in your code, you are iterating through the product table and getting the values in the match variable. In the part of the if statement where match is not null, you are setting the object to the new values, but you are not calling the dc.SubmitChanges();, that object match is not getting stored any where in your code, and in the next iteration in the loop, it is being assigned the new values.

    You need to call dc.SubmitChanges(); after update the match values.

    foreach (abcProduct p in abcProducts)
                    {
    
                        var match = (from t in dc.abcProducts
                                     where t.sku == p.productcode
                                     select t).FirstOrDefault();
    
                        if (match == null)
                        {
                           //insertion code commented out
                            dc.abcProducts.InsertOnSubmit(prod);
                        }
                        else
                        {
                            ///.... setting up all fields. 
                            match.stockPublished = false;
                            match.imgPublished = false;
                            match.prodPublished = false;
                            match.lastUpdated = DateTime.Now;
                            dc.SubmitChanges(); //Call this otherwise you will
                                                //loose the match values in the next iteration
                        }
                    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good morning. I've been having this issue for some days and have been lokking
Morning, I am having an issue adding up some totals using jQuery. I am
I have made some progress with the DataList and UserControl this morning but I
Morning stackoverflow, I have a repeater, with the following code in my aspx page;
Good Morning. I have two tables, and one references the other. When I insert
I have some jsp pages that connect to a mysql database and I intermittently
I have been stuck with this issue now all morning. I actually saw this
I have an issue that's been bugging me this morning. I'm building an ASP.NET
Have some Perl code which is using the DBI module - (the code is
Good Morning Stack OverFlow enthusiasts. I have been having an issue that I am

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.