CRM 2016: Error CS0103: The name 'item' does not exist in the current context

I am working on C# development for a CRM 2016 on premise project.  The solution includes classes, plugins, workflows and unit test projects.  

The problem I am having is with code taken from the Order class (which is working) that submits information to a mainframe for paint colors.  

I want to replicate the Order code in the Change Order class.  Most of the Order code copies over to the Change Order class without error,  but I am getting the "Error CS0103: The name 'item' does not exist in the current context" message for 6 lines of code.

I  have tried adding a Using statement, adding a reference, reviewing Stack Overflow suggestions but so far no success in clearing the errors.

I have included the Order class code (working version) and the Change Order code (with errors) below.  I have added comments (//ERROR ON LINE BELOW)  to the 6 lines of Change Order code where I getting the error on the 'item' reference

This is urgent and I am open to online sessions if that is required.

Thank you

ORDER CLASS CODE (NO ERRORS)
                     
#region Paint 
                            string paintCategory = item.GetAttributeValue<string>("uxce_category");
                              //1
                                string NarrativePaintColor = item.GetAttributeValue<string>("uxce_paintcolorselectionnarr");
                                if (!String.IsNullOrEmpty(NarrativePaintColor))
                                {
                                    //Remove trailing ';'
                                    if (NarrativePaintColor.EndsWith(";"))
                                    {
                                        NarrativePaintColor = NarrativePaintColor.Substring(0, NarrativePaintColor.Length - 1);
                                    }
                                    var results = nb.GeneratePaintNarrativeArray(NarrativePaintColor);

                                    //loop through each array item and apply the narrative code/desc
                                    for (int a = 0; a < results.Length; a++)
                                    {
                                        nb.AddNarrative(results[a][0], "N", results[a][1]);
                                    }
                                }
                                //2
                                string NarrativePaintDesign = item.GetAttributeValue<string>("uxce_paintcolorselectionnarr");
                                if (!String.IsNullOrEmpty(NarrativePaintDesign))
                                {
                                    string productdesc = item.GetAttributeValue<string>("productdescription");
                                    string optionCd = item.GetAttributeValue<string>("uxce_salesoption");

                                if (!String.IsNullOrEmpty(paintCategory) && paintCategory == "Paint Design")
                                {
                                    if (divisionCode == "K")
                                    {
                                        if (productdesc.Contains("Spectrum"))
                                        {
                                            nb.AddNarrative("1", "P", "SPECTRUM DESIGN CODE: " + optionCd);
                                            nb.AddNarrative("2", "P", "SPECTRUM DESIGN CODE: " + optionCd);
                                        }
                                        else
                                        {
                                            nb.AddNarrative("1", "P", "DESIGN CODE: " + optionCd);
                                            nb.AddNarrative("2", "P", "DESIGN CODE: " + optionCd);
                                        }
                                    }

                                    else if (divisionCode == "P")
                                    {
                                        if (productdesc.Contains("Feature"))
                                        {
                                            nb.AddNarrative("1", "P", "FEATURE DESIGN CODE: " + optionCd);
                                            nb.AddNarrative("2", "P", "FEATURE DESIGN CODE: " + optionCd);
                                        }
                                        else
                                        {
                                            nb.AddNarrative("1", "P", "CUSTOM DESIGN CODE: " + optionCd);
                                            nb.AddNarrative("2", "P", "CUSTOM DESIGN CODE: " + optionCd);
                                        }
                                    }
                                }
                                   
                                }

                                //#3
                                string NarrativePaintSpec = item.GetAttributeValue<string>("uxce_paintspecificationsnarr");
                                if (!String.IsNullOrEmpty(NarrativePaintSpec))
                                {                         
                                    if (NarrativePaintSpec.EndsWith(";"))
                                    {
                                        NarrativePaintSpec = NarrativePaintSpec.Substring(0, NarrativePaintSpec.Length - 1);
                                    }
                                    string[] SpecResults = NarrativePaintSpec.Split(';');
                                    char c1 = 'A';
                                    //loop through each array item and apply the narrative code/desc
                                    for (int a = 0; a < SpecResults.Length; a++)
                                    {
                                        nb.AddNarrative(c1.ToString(), "P", SpecResults[a]);
                                        c1++;

                                    }
                                
                            }
                            #endregion

Open in new window


CHANGE ORDER CODE (ERRORS)

       #region Paint
                    
                    //ERROR ON LINE BELOW
                    string paintCategory = item.GetAttributeValue<string>("uxce_category");

                    //1
                    //ERROR ON LINE BELOW
                   string NarrativePaintColor = item.GetAttributeValue<string>("uxce_paintcolorselectionnarr");
                    if (!String.IsNullOrEmpty(NarrativePaintColor))
                    {
                        //Remove trailing ';'
                        if (NarrativePaintColor.EndsWith(";"))
                        {
                            NarrativePaintColor = NarrativePaintColor.Substring(0, NarrativePaintColor.Length - 1);
                        }


                        var results = nb.GeneratePaintNarrativeArray(NarrativePaintColor);

                        //loop through each array item and apply the narrative code/desc
                        for (int a = 0; a < results.Length; a++)
                        {
                            nb.AddNarrative(results[a][0], "N", results[a][1]);

                        }
                    }
                    //2
                    //ERROR ON LINE BELOW
                    string NarrativePaintDesign = item.GetAttributeValue<string>("uxce_paintcolorselectionnarr");
                    if (!String.IsNullOrEmpty(NarrativePaintDesign))
                    {   
                    //ERROR ON LINE BELOW
                     string productdesc = item.GetAttributeValue<string>("productdescription");
                    //ERROR ON LINE BELOW
                      string optionCd = item.GetAttributeValue<string>("uxce_salesoption");

                        if (!String.IsNullOrEmpty(paintCategory) && paintCategory == "Paint Design")
                        {

                            if (divisionCode == "K")
                            {

                                if (productdesc.Contains("Spectrum"))
                                {
                                    nb.AddNarrative("1", "P", "SPECTRUM DESIGN CODE: " + optionCd);
                                    nb.AddNarrative("2", "P", "SPECTRUM DESIGN CODE: " + optionCd);
                                }
                                else
                                {
                                    nb.AddNarrative("1", "P", "DESIGN CODE: " + optionCd);
                                    nb.AddNarrative("2", "P", "DESIGN CODE: " + optionCd);
                                }
                            }

                            else if (divisionCode == "P")
                            {
                                if (productdesc.Contains("Feature"))
                                {
                                    nb.AddNarrative("1", "P", "FEATURE DESIGN CODE: " + optionCd);
                                    nb.AddNarrative("2", "P", "FEATURE DESIGN CODE: " + optionCd);
                                }
                                else
                                {
                                    nb.AddNarrative("1", "P", "CUSTOM DESIGN CODE: " + optionCd);
                                    nb.AddNarrative("2", "P", "CUSTOM DESIGN CODE: " + optionCd);
                                }
                            }
                        }


                    }

                    //#3
                    //ERROR ON LINE BELOW
                    string NarrativePaintSpec = item.GetAttributeValue<string>("uxce_paintspecificationsnarr");

Open in new window

LVL 1
apollo7Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

p_davisCommented:
where is 'item' defined... we need to see if it is in scope.
0
apollo7Author Commented:
I do not see a place in these two classes where item is defined, it is used in foreach loops and referenced as shown below:

foreach (PACCAR_CRM_Code.Library.ProxyClasses.OrderProduct item in orderProductsEntity)
                    {
                        if (item.SalesOption == null)
                        {
                            item.SalesOption = " ";
                            string desc = "";
                            if (item.ProductName != null)
                            {
                                desc = item.ProductName;
                            }
                            eventLog.WriteEntry
                                (
                                    "Sales Option EMPTY!" + "\n" + desc
                                    , EventLogEntryType.Error
                                );
                        }
                    }

Open in new window

0
p_davisCommented:
i would guess that you have a misplaced } 'closing bracket'. all references to 'item' would need to be contained withing the foreach loop.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

apollo7Author Commented:
I will take a look at that, would be great if this fixes it
0
apollo7Author Commented:
I don't see any brackets out of place although it may involve the placement of the code.  The weird thing is if I copy just one line from the Order class code to the Change Order class code, I get the red underline on the 'item' reference:

string paintCategory = item.GetAttributeValue<string>("uxce_category");
0
p_davisCommented:
are there any other errors?
are you missing a using statement or a references for the new class?
0
apollo7Author Commented:
No other errors, I compared the using statements between Order and Change Order classes and added a missing one.  The references are the same in both classes.

May not have done a build after adding the using statement to Change Order
0
Chris StanyonWebDevCommented:
Unfortunately, you're only showing us a part of the code so it's impossible to see exactly what's going on.

Before the code you've shown, there must be somewhere item is defined. It could be part of a foreach loop / it could be a private field / it could be injected into your method. You say it's only defined in the foreach loop, but the foreach loop you've posted then makes no reference to the code you have errors in, so it must be defined somewhere else.

If you're using Visual Studio, put your cursor over the item and press F12 - this should jump you to the definition and allow you to track it from there. If you're using a different IDE, then you should have another way of visiting a definition.

You may have to post up your full class (the working one and the non-working one)
0
apollo7Author Commented:
Chris, thanks for your response, I will use the F12 option to track down the definition.  If needed, I can show both full classes but I will try F12 first.
0
apollo7Author Commented:
Ok that allowed me to track it down.  The hover-over took me to orderProducts and in that foreach loop, item is called option.  By changing item to option and wrapping in a foreach loop, the problem went away and I was able to build without any errors.

Thanks
0
Chris StanyonWebDevCommented:
Nice :)
0
apollo7Author Commented:
Do you have any objection to splitting the points with p_davis?  I like to reward both experts when more than one puts in their time.
0
Chris StanyonWebDevCommented:
No objection from me :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
apollo7Author Commented:
Thank you both for sticking with this issue and getting it resolved.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.