I am trying to convert this block of code to Async
public static List<Employee> EmployeesList()
{
List<Employee> list = new List<Employee>();
DataTable dt = new DataTable();
dt = Data.DataAccess.ExecuteDataTable("[Employee].[dbo].[Employee_Select]");
foreach (DataRow row in dt.Rows)
{
list.Add(new Employee()
{
Id = Convert.ToInt32(row["ID"]),
Name = Convert.ToString(row["Name"]),
Email = Convert.ToString(row["Email"]),
Age = Convert.ToInt32(row["Age"]),
Salary = Convert.ToInt32(row["Salary"])
});
}
return list;
}
The modified code looks like this
public static async Task<List<Employee>> EmployeesList()
{
List<Employee> list = new List<Employee>();
DataTable dt = new DataTable();
dt = await Data.DataAccess.ExecuteDataTableAsync("[Employee].[dbo].[Employee_Select]");
foreach (DataRow row in dt.Rows)
{
list.Add(new Employee()
{
Id = Convert.ToInt32(row["ID"]),
Name = Convert.ToString(row["Name"]),
Email = Convert.ToString(row["Email"]),
Age = Convert.ToInt32(row["Age"]),
Salary = Convert.ToInt32(row["Salary"])
});
}
return list;
}
Edit for clarity. The synchronous method returns a list. The asynchronous method does not (It just exits the block and skipping the break points under the await), which causes a null reference error further down the path. Why isn't my Asynchronous method returning any results?
Ignore this part from the original post. The problem I am having is that my async code block seems to be exiting before the data is returned because the Razor page gives me an Object reference not set to an instance of an object error on my Model.Employees. What am I doing wrong?