How to return only employeid without details request ?

Ahmed Salah Abed Elaziz 390 Reputation points
2023-04-02T07:24:08.7033333+00:00

I work on blazor server side I face issues i can't return only employeeid from web api request without request details

so i need to return data from web api reuest as array of "employeeID": "212334" etc

and no need to return details of request as ( id ,exception,status,iscancelled,iscompleted,iscompletedsuccesfully,asyncState,

isFaulted,creationOptions

only need return array of employeeid

web api return data i need to modify it as below :

[HttpGet]
        [Route("GetAllEmployee/{searchText}")]
        public async Task<IActionResult> GetAllEmployee(string searchText)
        {
          
            string query = "";

          
            query = "SELECT cast(EMP.YAAN8 as varchar(20)) as EmployeeID,EMP.YAALPH AS EmployeeName FROM CRPDTA.F060116 EMP WHERE cast(EMP.YAAN8 as varchar(20)) LIKE '%" + searchText + "%' WITH UR";


            try
            {
                ICollection<object> ApplicationsDataValues = new List<object>();
                using (var command = con.CreateCommand())
                {
                    command.CommandText = query;
                    command.CommandType = CommandType.Text;


                    await command.Connection.OpenAsync();

                    using (var reader = await command.ExecuteReaderAsync())
                    {
                        while (await reader.ReadAsync())
                        {
                            ApplicationsDataValues.Add(new
                            {
                                EmployeeID = reader.GetFieldValueAsync<string>(0)


                            });
                        }
                    }
                }
                await  con.CloseAsync();
                return StatusCode(200, ApplicationsDataValues); // Get all users   
            }
            catch (Exception e)
            {
                return StatusCode(500, e);
            }
           
        }

it return data as below :

[
    {
        "employeeID": {
            "result": "212334",
            "id": 251,
            "exception": null,
            "status": 5,
            "isCanceled": false,
            "isCompleted": true,
            "isCompletedSuccessfully": true,
            "creationOptions": 0,
            "asyncState": null,
            "isFaulted": false
        }
    },
    {
        "employeeID": {
            "result": "212331",
            "id": 252,
            "exception": null,
            "status": 5,
            "isCanceled": false,
            "isCompleted": true,
            "isCompletedSuccessfully": true,
            "creationOptions": 0,
            "asyncState": null,
            "isFaulted": false
        }
    },
    {
        "employeeID": {
            "result": "271233",
            "id": 253,
            "exception": null,
            "status": 5,
            "isCanceled": false,
            "isCompleted": true,
            "isCompletedSuccessfully": true,
            "creationOptions": 0,
            "asyncState": null,
            "isFaulted": false
        }
    }

expected result returned

[
    {
        "employeeID": "212334"
    },
    {
        "employeeID": "212331"
    },
    {
        "employeeID": "271233"
    }
]
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,400 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,500 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
318 questions
{count} votes

Accepted answer
  1. Farid Uddin Kiron MSFT 456 Reputation points Microsoft Vendor
    2023-04-03T02:57:25.2766667+00:00

    I need to return data from web api reuest as array of "employeeID": "212334" etc

    Actually as you are using GetFieldValueAsync method which returns TResult object as following:

    ReturnsType

    Thus, if you don't use awaiter for asynchronous operation compiler internally throws error and returns its generic result which I have reproduce accordingly.

    Issue Reproduced:

    IssueReproduced

    Solution:

    In order to get rid of unexpected result you could do either of following steps:

    1. Put awaiter while binding your employee in other words, instead of reader.GetFieldValueAsync<string>(0) write await reader.GetFieldValueAsync<string>(0)
    2. Or you can simply write  EmployeeID = reader.GetValue(0)

    Either of above approach would resolve your issue.

    Output:

    Solutions

    Note: If you would like to know more details on it you could check our official document here

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. AgaveJoe 27,696 Reputation points
    2023-04-02T12:33:29.1866667+00:00

    You forgot the await.

    EmployeeID = await reader.GetFieldValueAsync<string>(0)
    
    0 comments No comments

  2. Bruce (SqlWork.com) 61,731 Reputation points
    2023-04-02T16:16:24.4366667+00:00

    Because you forgot the await it is returning the serialized array of tasks, not values.

    had you used strong typing, the compiler would of caught the error.

    0 comments No comments