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

  • Home
  • SEARCH
  • 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 6866673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:12:54+00:00 2026-05-27T03:12:54+00:00

I have a method where I READ objects from DB, for instance: public Object

  • 0

I have a method where I READ objects from DB, for instance:

public Object getProduct(int categoryId, int productId)
{
        DataClassesDataContext db = new DataClassesDataContext(Settings.getDefaultConnectionStringName());

        switch (categoryId)
        {
            case CCategorii.CARTI_ID:
                {
                    IEnumerable<Carti> product = (from c in db.Cartis
                                                  where c.Carti_id == productId
                                                  && c.Vizibil == true
                                                  select c);

                    if (product.Count() != 0)
                        return product.First();

                    break;
                }
           //so on
        }
}

Now I have another method where I do the update:

public void updateProduct()
{
   Object productToBeUpdated = getProduct(1,1);
    DataClassesDataContext db = new DataClassesDataContext(Settings.getDefaultConnectionStringName());
   //update some properties of the product
   productToBeUpdated.setQuantity(productToBeUpdated.getQuantity()+1);
   db.submitChanges();
}

Well, the product was succcesfully read from previous method but changes were not done into the DB.

I think the cause is that I do this READ-UPDATE in two different DataContext…If this is the cause how do you threat this situations?

Oh yeah, I can read the product and update in the same method but this means to duplicate the method I use for reading and add to it update stuff… and I would like to avoid this.

  • 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-27T03:12:54+00:00Added an answer on May 27, 2026 at 3:12 am

    You are using two different instances of your DataContext.

    When implementing a web app, the best option is usually to align the lifetime of your DataContext to the lifetime of one http request. The lifetime you use is just too short.

    Another option is to attach the object to the write DataContext:

    db.Cartis.Attach(yourReadObject);
    updateProperties(yourReadObject);
    db.submitChanges();
    

    EDIT

    Ok, you have to detach the object from your other context first. See this article on how to do it.

    But i really would recommend to use a single DataContext object and extend the lifetime to the httprequest scope.

    This can be done really nice with an ioc container like autofac.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multithreaded Java code in which: several threads read stateful objects from
I have a C# database layer that with static read method that is called
I have to read a file in the BUILD method and I want to
I have a method which takes params object[] such as: void Foo(params object[] items)
New to the object orientated method of doing things and was just wondering what
I have read about partial methods in the latest C# language specification , so
I have read this post about how to test private methods. I usually do
I have read an example and tried to duplicate it's methods but with weird
I have method in a class that I need to make sure is only
I have class method that returns a list of employees that I can iterate

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.