c# Object properties is loading from cache











up vote
0
down vote

favorite












in my win-form application, there is a method that combines some items that have been created previously, when the code is the first time to run, everything is fine, but in second and later runs combined items have the wrong length.



the code reads items from a SQL server using LINQ that has object type named "BetaData"



BetaData has a property named "Length" that is double.
I have another list that processed items is stored in name "PartList" of type "ModifiedPartList".



in method length property changes for some items, but nothing gets stored or saved on SQL.
this is the main method:



private List<ModifiedPartList> CombinePartList(ProgressBar Bar)
{
PartList.Clear();

List<BetaData> PartsinOrder = new List<BetaData>();
foreach (int view in Globals.Views)
{
List<int> OrdersInView = new List<int>();
foreach (Tuple<int, int> tuple in Globals.Orders)
{
if (tuple.Item1 == view)
{
if (!OrdersInView.Contains(tuple.Item2))
OrdersInView.Add(tuple.Item2);
}
}
if(OrdersInView.Count>0)
{
OrdersInView.Sort();
foreach (int order in OrdersInView)
{
//this is the section that problem occurs:
var parts = from BetaData in BetaContext.BetaDatas
where BetaData.ProjectName == Globals.ProjectName &&
BetaData.ProjectCode == Globals.ProjectCode &&
BetaData.ParentItem != Globals.ProjectName + "(" + Globals.ProjectCode + ")" &&
BetaData.View == view &&
BetaData.Order == order
select BetaData;
PartsinOrder.Clear();
PartsinOrder = parts.ToList();
foreach(BetaData part in PartsinOrder)
{

Bar.PerformStep();
}
}

}
PartsinOrder.Clear();
}
return PartList;
}


in the section that i have commented as problem location when the code is running for the second time, optimized length property is loaded to items instead of their original value from SQL. i cannot understand that because each time i read all items from SQL server.
the point is in this stage after that i ran the method for several times and getting the wrong results when i close the program and start it again, on first run results are true.
after selecting from SQL and converting it to list, i review items and their properties in list, and they are all true, but in foreach loop when each part comes into loop their Length property is wrong.










share|improve this question
























  • Have you tried debugging the method? Also, you haven't posted the code where you do the actual saving of the changes.
    – Peter Bons
    yesterday










  • And may I advice you to reduce the complexity of the CombinePartList method by splitting it up into smaller methods? The length and amount of nesting due to the many if/else constructs makes it very difficult to read and understand.
    – Peter Bons
    yesterday










  • I can see a few calls to SaveLength(). Is it possible you're overwriting the values in the database so when you run it for the second time you're actually loading those saved/changed values back in?
    – Handbag Crab
    yesterday










  • @PeterBons yes i have, there is no residuals after running the method that may affect next run, i have debugged it step by step, and when sql selection is running, everything is fine, but when objects are passed into foreach loop they get their last edited value.
    – Reza
    yesterday










  • @HandbagCrab as you can see in begining of the method each time method runs "ClearSavedLengthes();" clear all parameters that have bin saved.
    – Reza
    yesterday















up vote
0
down vote

favorite












in my win-form application, there is a method that combines some items that have been created previously, when the code is the first time to run, everything is fine, but in second and later runs combined items have the wrong length.



the code reads items from a SQL server using LINQ that has object type named "BetaData"



BetaData has a property named "Length" that is double.
I have another list that processed items is stored in name "PartList" of type "ModifiedPartList".



in method length property changes for some items, but nothing gets stored or saved on SQL.
this is the main method:



private List<ModifiedPartList> CombinePartList(ProgressBar Bar)
{
PartList.Clear();

List<BetaData> PartsinOrder = new List<BetaData>();
foreach (int view in Globals.Views)
{
List<int> OrdersInView = new List<int>();
foreach (Tuple<int, int> tuple in Globals.Orders)
{
if (tuple.Item1 == view)
{
if (!OrdersInView.Contains(tuple.Item2))
OrdersInView.Add(tuple.Item2);
}
}
if(OrdersInView.Count>0)
{
OrdersInView.Sort();
foreach (int order in OrdersInView)
{
//this is the section that problem occurs:
var parts = from BetaData in BetaContext.BetaDatas
where BetaData.ProjectName == Globals.ProjectName &&
BetaData.ProjectCode == Globals.ProjectCode &&
BetaData.ParentItem != Globals.ProjectName + "(" + Globals.ProjectCode + ")" &&
BetaData.View == view &&
BetaData.Order == order
select BetaData;
PartsinOrder.Clear();
PartsinOrder = parts.ToList();
foreach(BetaData part in PartsinOrder)
{

Bar.PerformStep();
}
}

}
PartsinOrder.Clear();
}
return PartList;
}


in the section that i have commented as problem location when the code is running for the second time, optimized length property is loaded to items instead of their original value from SQL. i cannot understand that because each time i read all items from SQL server.
the point is in this stage after that i ran the method for several times and getting the wrong results when i close the program and start it again, on first run results are true.
after selecting from SQL and converting it to list, i review items and their properties in list, and they are all true, but in foreach loop when each part comes into loop their Length property is wrong.










share|improve this question
























  • Have you tried debugging the method? Also, you haven't posted the code where you do the actual saving of the changes.
    – Peter Bons
    yesterday










  • And may I advice you to reduce the complexity of the CombinePartList method by splitting it up into smaller methods? The length and amount of nesting due to the many if/else constructs makes it very difficult to read and understand.
    – Peter Bons
    yesterday










  • I can see a few calls to SaveLength(). Is it possible you're overwriting the values in the database so when you run it for the second time you're actually loading those saved/changed values back in?
    – Handbag Crab
    yesterday










  • @PeterBons yes i have, there is no residuals after running the method that may affect next run, i have debugged it step by step, and when sql selection is running, everything is fine, but when objects are passed into foreach loop they get their last edited value.
    – Reza
    yesterday










  • @HandbagCrab as you can see in begining of the method each time method runs "ClearSavedLengthes();" clear all parameters that have bin saved.
    – Reza
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











in my win-form application, there is a method that combines some items that have been created previously, when the code is the first time to run, everything is fine, but in second and later runs combined items have the wrong length.



the code reads items from a SQL server using LINQ that has object type named "BetaData"



BetaData has a property named "Length" that is double.
I have another list that processed items is stored in name "PartList" of type "ModifiedPartList".



in method length property changes for some items, but nothing gets stored or saved on SQL.
this is the main method:



private List<ModifiedPartList> CombinePartList(ProgressBar Bar)
{
PartList.Clear();

List<BetaData> PartsinOrder = new List<BetaData>();
foreach (int view in Globals.Views)
{
List<int> OrdersInView = new List<int>();
foreach (Tuple<int, int> tuple in Globals.Orders)
{
if (tuple.Item1 == view)
{
if (!OrdersInView.Contains(tuple.Item2))
OrdersInView.Add(tuple.Item2);
}
}
if(OrdersInView.Count>0)
{
OrdersInView.Sort();
foreach (int order in OrdersInView)
{
//this is the section that problem occurs:
var parts = from BetaData in BetaContext.BetaDatas
where BetaData.ProjectName == Globals.ProjectName &&
BetaData.ProjectCode == Globals.ProjectCode &&
BetaData.ParentItem != Globals.ProjectName + "(" + Globals.ProjectCode + ")" &&
BetaData.View == view &&
BetaData.Order == order
select BetaData;
PartsinOrder.Clear();
PartsinOrder = parts.ToList();
foreach(BetaData part in PartsinOrder)
{

Bar.PerformStep();
}
}

}
PartsinOrder.Clear();
}
return PartList;
}


in the section that i have commented as problem location when the code is running for the second time, optimized length property is loaded to items instead of their original value from SQL. i cannot understand that because each time i read all items from SQL server.
the point is in this stage after that i ran the method for several times and getting the wrong results when i close the program and start it again, on first run results are true.
after selecting from SQL and converting it to list, i review items and their properties in list, and they are all true, but in foreach loop when each part comes into loop their Length property is wrong.










share|improve this question















in my win-form application, there is a method that combines some items that have been created previously, when the code is the first time to run, everything is fine, but in second and later runs combined items have the wrong length.



the code reads items from a SQL server using LINQ that has object type named "BetaData"



BetaData has a property named "Length" that is double.
I have another list that processed items is stored in name "PartList" of type "ModifiedPartList".



in method length property changes for some items, but nothing gets stored or saved on SQL.
this is the main method:



private List<ModifiedPartList> CombinePartList(ProgressBar Bar)
{
PartList.Clear();

List<BetaData> PartsinOrder = new List<BetaData>();
foreach (int view in Globals.Views)
{
List<int> OrdersInView = new List<int>();
foreach (Tuple<int, int> tuple in Globals.Orders)
{
if (tuple.Item1 == view)
{
if (!OrdersInView.Contains(tuple.Item2))
OrdersInView.Add(tuple.Item2);
}
}
if(OrdersInView.Count>0)
{
OrdersInView.Sort();
foreach (int order in OrdersInView)
{
//this is the section that problem occurs:
var parts = from BetaData in BetaContext.BetaDatas
where BetaData.ProjectName == Globals.ProjectName &&
BetaData.ProjectCode == Globals.ProjectCode &&
BetaData.ParentItem != Globals.ProjectName + "(" + Globals.ProjectCode + ")" &&
BetaData.View == view &&
BetaData.Order == order
select BetaData;
PartsinOrder.Clear();
PartsinOrder = parts.ToList();
foreach(BetaData part in PartsinOrder)
{

Bar.PerformStep();
}
}

}
PartsinOrder.Clear();
}
return PartList;
}


in the section that i have commented as problem location when the code is running for the second time, optimized length property is loaded to items instead of their original value from SQL. i cannot understand that because each time i read all items from SQL server.
the point is in this stage after that i ran the method for several times and getting the wrong results when i close the program and start it again, on first run results are true.
after selecting from SQL and converting it to list, i review items and their properties in list, and they are all true, but in foreach loop when each part comes into loop their Length property is wrong.







c# winforms oop object






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 18 hours ago

























asked yesterday









Reza

113




113












  • Have you tried debugging the method? Also, you haven't posted the code where you do the actual saving of the changes.
    – Peter Bons
    yesterday










  • And may I advice you to reduce the complexity of the CombinePartList method by splitting it up into smaller methods? The length and amount of nesting due to the many if/else constructs makes it very difficult to read and understand.
    – Peter Bons
    yesterday










  • I can see a few calls to SaveLength(). Is it possible you're overwriting the values in the database so when you run it for the second time you're actually loading those saved/changed values back in?
    – Handbag Crab
    yesterday










  • @PeterBons yes i have, there is no residuals after running the method that may affect next run, i have debugged it step by step, and when sql selection is running, everything is fine, but when objects are passed into foreach loop they get their last edited value.
    – Reza
    yesterday










  • @HandbagCrab as you can see in begining of the method each time method runs "ClearSavedLengthes();" clear all parameters that have bin saved.
    – Reza
    yesterday


















  • Have you tried debugging the method? Also, you haven't posted the code where you do the actual saving of the changes.
    – Peter Bons
    yesterday










  • And may I advice you to reduce the complexity of the CombinePartList method by splitting it up into smaller methods? The length and amount of nesting due to the many if/else constructs makes it very difficult to read and understand.
    – Peter Bons
    yesterday










  • I can see a few calls to SaveLength(). Is it possible you're overwriting the values in the database so when you run it for the second time you're actually loading those saved/changed values back in?
    – Handbag Crab
    yesterday










  • @PeterBons yes i have, there is no residuals after running the method that may affect next run, i have debugged it step by step, and when sql selection is running, everything is fine, but when objects are passed into foreach loop they get their last edited value.
    – Reza
    yesterday










  • @HandbagCrab as you can see in begining of the method each time method runs "ClearSavedLengthes();" clear all parameters that have bin saved.
    – Reza
    yesterday
















Have you tried debugging the method? Also, you haven't posted the code where you do the actual saving of the changes.
– Peter Bons
yesterday




Have you tried debugging the method? Also, you haven't posted the code where you do the actual saving of the changes.
– Peter Bons
yesterday












And may I advice you to reduce the complexity of the CombinePartList method by splitting it up into smaller methods? The length and amount of nesting due to the many if/else constructs makes it very difficult to read and understand.
– Peter Bons
yesterday




And may I advice you to reduce the complexity of the CombinePartList method by splitting it up into smaller methods? The length and amount of nesting due to the many if/else constructs makes it very difficult to read and understand.
– Peter Bons
yesterday












I can see a few calls to SaveLength(). Is it possible you're overwriting the values in the database so when you run it for the second time you're actually loading those saved/changed values back in?
– Handbag Crab
yesterday




I can see a few calls to SaveLength(). Is it possible you're overwriting the values in the database so when you run it for the second time you're actually loading those saved/changed values back in?
– Handbag Crab
yesterday












@PeterBons yes i have, there is no residuals after running the method that may affect next run, i have debugged it step by step, and when sql selection is running, everything is fine, but when objects are passed into foreach loop they get their last edited value.
– Reza
yesterday




@PeterBons yes i have, there is no residuals after running the method that may affect next run, i have debugged it step by step, and when sql selection is running, everything is fine, but when objects are passed into foreach loop they get their last edited value.
– Reza
yesterday












@HandbagCrab as you can see in begining of the method each time method runs "ClearSavedLengthes();" clear all parameters that have bin saved.
– Reza
yesterday




@HandbagCrab as you can see in begining of the method each time method runs "ClearSavedLengthes();" clear all parameters that have bin saved.
– Reza
yesterday












1 Answer
1






active

oldest

votes

















up vote
1
down vote













the issue was solved using this article and refreshing context after retrieving data from SQL






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236874%2fc-sharp-object-properties-is-loading-from-cache%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    the issue was solved using this article and refreshing context after retrieving data from SQL






    share|improve this answer

























      up vote
      1
      down vote













      the issue was solved using this article and refreshing context after retrieving data from SQL






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        the issue was solved using this article and refreshing context after retrieving data from SQL






        share|improve this answer












        the issue was solved using this article and refreshing context after retrieving data from SQL







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 18 hours ago









        Reza

        113




        113






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236874%2fc-sharp-object-properties-is-loading-from-cache%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."