-2

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?

Buck Hicks
  • 1,534
  • 16
  • 27
  • 2
    Please re-read the [mre] guidance and [edit] the post - code in the post *does not* ever return `null` nor set `Model.Employees` property. – Alexei Levenkov Feb 10 '23 at 00:11

1 Answers1

1

What is your calling code to this method? Once you enter into async\await, you need to await all the calling methods. You need to await EmployeeList(). Otherwise the calling method prematurely given control and your list is always null.

Hope this helps

  • I am using an await on th call: public async void OnGet() { Employees = await EmployeesList(); } – Buck Hicks Feb 10 '23 at 01:15
  • Similar thing happened to me. When my controller return type is void, the calling method will not await. When I change return type to Task, it will await and I will not get null exception. – Sundar Babu Manoharan Feb 10 '23 at 01:17
  • 1
    @BuckHicks You should check out [this question](https://stackoverflow.com/questions/12144077/async-await-when-to-return-a-task-vs-void). `Async void` is very rarely valid. – mason Feb 10 '23 at 01:20
  • There you go. Change void to Task. Please refer here. https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming – Sundar Babu Manoharan Feb 10 '23 at 01:21
  • That was it. I had just changed that to a task because your post and was coming back here to update this, Thank you. – Buck Hicks Feb 10 '23 at 01:23