Link to home
Start Free TrialLog in
Avatar of apollo7
apollo7Flag for United States of America

asked on

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

Avatar of p_davis
p_davis

where is 'item' defined... we need to see if it is in scope.
Avatar of apollo7

ASKER

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

i would guess that you have a misplaced } 'closing bracket'. all references to 'item' would need to be contained withing the foreach loop.
Avatar of apollo7

ASKER

I will take a look at that, would be great if this fixes it
Avatar of apollo7

ASKER

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");
SOLUTION
Avatar of p_davis
p_davis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of apollo7

ASKER

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
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)
Avatar of apollo7

ASKER

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.
Avatar of apollo7

ASKER

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
Nice :)
Avatar of apollo7

ASKER

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of apollo7

ASKER

Thank you both for sticking with this issue and getting it resolved.