How to resolve the declation of TempData[""] in a static field?

yguyon28 used Ask the Experts™
Why can''t I use a TempData within a static method?

I'm getting the following error on this line of code :   empData["ss"] = resultsContainer

C# An object reference is required for the non-static field, method, or property

      public static XtraReport BuildReportObject(DailyEmployeeRecapSearchCriteria searchCriteria
            , Models.Employee LoggedInUser
            , System.Globalization.CultureInfo systemCultureInfo
            , ref XtraReport mainReport
            var resultsContainer = xxx.CustomReports.DailyEmployeeRecapService.GetReportResults(searchCriteria, LoggedInUser.CultureInfo, LoggedInUser.ID.Trim());
            resultsContainer.CustomerDetails.Select(s => s.CustomerNumberAndName = s.CustomerNumber.ToString().Trim() + " - " + (s.CustomerFirstNameLastNameOrCompany ?? "").Trim()).ToList();
            resultsContainer.CustomerDetails.Select(s => s.CustomerNumberAndName += (s.NewCustomer ? " - " + Tools.Language.Translate(Language.Keys.New, LoggedInUser.CultureInfo) + "!" : "")).ToList();

            empData["ss"] = resultsContainer

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Distinguished Expert 2017
Hi yguyon28;
From Microsoft documentation:
A non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.

So empData must be sent in as a parameter to use it.


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial