The name 'xyz' does not exist in the current context

I'm getting the following error: The name does not exist in the current context. in the following 2 lines bellow after I implement my if else statement. Not sure if there is a better way to write this ifelse statement as a tenary statement...

            var resultsContainer = GetReportResultsObject(searchCriteria, LoggedInUser, systemCultureInfo);
            var resultsContainer2 = GetReportResultsObject2(searchCriteria2, LoggedInUser, systemCultureInfo);


public ActionResult GenerateReportFromCache(string guidToken)
        {
            var objResponse = MemoryCache.Default.Get(guidToken) as ResponseObject;
            
            if (objResponse.Model.GetType().Equals(typeof(xxx.Models.CustomReports.DailyTechnicianRecapSearchCriteria)))
            {
                var searchCriteria = objResponse.Model as DailyEmployeeRecapSearchCriteria;
            }
            else
            {
                var searchCriteria2 = objResponse.Model as DailyTechnicianRecapSearchCriteria;
            }
           
           var systemCultureInfo = (System.Globalization.CultureInfo)ViewBag.SystemCultureInfo;

            var resultsContainer = GetReportResultsObject(searchCriteria, LoggedInUser, systemCultureInfo);
            var resultsContainer2 = GetReportResultsObject2(searchCriteria2, LoggedInUser, systemCultureInfo);


            XtraReport mainReport = new XtraReport();
            if (searchCriteria.ReportByTechnician)
            {
                DailyTechnicianRecapSearchCriteria techCriteria = new DailyTechnicianRecapSearchCriteria();
                techCriteria.Map(searchCriteria);
                techCriteria.FavoriteReportName = CustomReportsNames.DailyTechnicianRecap;
                mainReport = BuildTechnicianReportObject(techCriteria, LoggedInUser, systemCultureInfo);
            }
            else
            {
                searchCriteria.FavoriteReportName = CustomReportsNames.DailyEmployeeRecap;
                BuildReportObject(searchCriteria, LoggedInUser, systemCultureInfo, ref mainReport);
            }

            return GenerateReport(mainReport, searchCriteria.PreviewFlag);
        }

Open in new window

yguyon28Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

AndyAinscowFreelance programmer / ConsultantCommented:
Your problem is with the lifetime of the variables:

{
                var searchCriteria = objResponse.Model as DailyEmployeeRecapSearchCriteria;
            }  //After going past this brace the variable searchCriteria is DESTROYED


You need something like this:
var searchCriteria;
var searchCriteria2;
            if (objResponse.Model.GetType().Equals(typeof(xxx.Models.CustomReports.DailyTechnicianRecapSearchCriteria)))
            {
                searchCriteria = objResponse.Model as DailyEmployeeRecapSearchCriteria;
            }
            else
            {
                searchCriteria2 = objResponse.Model as DailyTechnicianRecapSearchCriteria;
            }
AndyAinscowFreelance programmer / ConsultantCommented:
Note it still will give you problems because only one will be given a value BUT you attempt to use both later.
yguyon28Author Commented:
Yes that is my problem.. The logic is wrong.
Dustin SaundersDirector of OperationsCommented:
Seems like you could implement an Interface on these objects and simplify a lot of this code, no?
AndyAinscowFreelance programmer / ConsultantCommented:
You need to sort out the logic yourself.  Your existing code does not make sense so I can't really suggest how to fix it.
You attempt to fill two variables when the if statement means only one search criteria is set.  Of those two variables one is never used at all later.

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
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.