Click here to Skip to main content
15,905,420 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This error occurs in the following method, please help


What I have tried:

[HttpPost]
   public async Task<IActionResult> DeatailsAnalysis(int carId, string selectedDate)
   {
       DateTime selectedDay = PersianToGregorian(selectedDate, out int selectedPersianYear);
       SqlConnection connection = null;
       SqlCommand command = null;
       var model = new List<InventoryAnalysisReportViewModel>();

       try
       {
           connection = DbContext.Database.GetDbConnection() as SqlConnection;
           command = new SqlCommand("dbo.DeatailsInventoryAnalysisReportPerDay")
           {
               CommandType = CommandType.StoredProcedure,
               Connection = connection
           };
           command.Parameters.Add(new SqlParameter("@SelectedDay", selectedDay));
           command.Parameters.Add(new SqlParameter("@CarId", carId));

           if (connection.State != ConnectionState.Open)
               await connection.OpenAsync();

           using var reader = await command.ExecuteReaderAsync();
           while (await reader.ReadAsync())
           {
               model.Add(new InventoryAnalysisReportViewModel
               {
                   CarId = reader.GetInt32(reader.GetOrdinal("CarID")),
                   CarTitle = reader.GetString(reader.GetOrdinal("CarTitle")),
                   ProvisionalDeliveryQty = reader.GetInt32(reader.GetOrdinal("ProvisionalDeliveryQty")),
                   SectionsQty = reader.GetInt32(reader.GetOrdinal("SectionsQty")),
                   ParkingInventoryQty = reader.GetInt32(reader.GetOrdinal("ParkingInventoryQty")),
                   //InventoryQty = reader.GetInt32(reader.GetOrdinal("InventoryQty")),
                   TotalDeatails = reader.GetInt32(reader.GetOrdinal("TotalDeatails")),
                   YearType = Convert.ToInt32(reader.GetValue(reader.GetOrdinal("CarModels"))) == selectedPersianYear
                               ? YearsType.Currentyear
                               : YearsType.Pastyears
               });
           }
       }
       catch (Exception ex)
       {
           throw ex;
       }
       finally
       {
           if (command != null)
           {
               command.Dispose();
           }
           if (connection.State != ConnectionState.Closed)
           {
               connection.Close();
               connection.Dispose();
           }
       }

       return Json(model);
   }
Posted
Updated 2-Oct-20 22:21pm
Comments
Patrice T 3-Oct-20 1:50am    
Give exact error message and tell line of error.

The error mesna exactly what it says: you are using a variable, property, or method name that does not exist in the current context.
It may have gone out of scope by the time you use it:
C#
while (a < b)
   {
   int c = Foo(a);
   a++;
   }
Bar(c);
By the time you call Bar, c has gone out of scope.
Or it may be that you have misspelled something.
Or perhaps you are missing a using statement.

We can't tell: we can't compile your code in isolation and get the same result you do.

So double click the error message and VS will take you directly to the error line. Look for a name that is underlined in red and then try to find it's definition, or where you think it is defined.

Sorry, but we can't do that for you!
 
Share this answer
 
C#
DateTime selectedDay = PersianToGregorian(selectedDate, out int selectedPersianYear);

I think that syntax is incorrect, but also you have not declared selectedPersianYear.
 
Share this answer
 
v2
Comments
Richard Deeming 5-Oct-20 10:56am    
That's a new syntax added in C# 7, which lets you declare an out variable inline:
What's New in C# 7.0 - C# Guide | Microsoft Docs[^]
Richard MacCutchan 5-Oct-20 10:58am    
:(
Member 14615938 5-Oct-20 11:57am    
help meeeee
Richard MacCutchan 5-Oct-20 12:35pm    
We cannot help you unless you provide all the information. What is the exact error message, and which line does it occur on?
ZurdoDev 5-Oct-20 13:37pm    
Strikethrough is a new c# 7 syntax?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900