Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Store procedure and multiple columns. (UserlistDto) #5837


User avatar
0
ashgadala created

This might not be directly related to ASPNETZERO problem. I appreciate if some one can help.

Support, I got the result that i am expecting with below implementation. Is this the best way to handle it Or Do you recommend a better way ? Appreciate any help.

I was able to successfully implement the GetUserNames() task by following the documentation.

I modified the store procedure to include all columns in the UserlistDTO and would like to return all the columns.

Implementation from documentation

public async Task<List<string>> GetUserNames()
{
    EnsureConnectionOpen();

    using (var command = CreateCommand("GetUsernames", CommandType.storedProcedure))
    {
        using (var dataReader = await command.ExecuteReaderAsync())
        {
            var result = new List<string>();

            while (dataReader.Read())
            {
                result.Add(dataReader["UserName"].ToString());
            }

            return result;
        }
    }
}

My Implementation based on Stackoverflow thread

This stackoverflow Thread case to rescue.

Here is the my UserRepository.

public async Task&lt;IEnumerable&lt;object&gt;> GetUserNamesarray()
   {
              using (var command = CreateCommand("GetUsernames", CommandType.StoredProcedure))
        {
            using (var dataReader = await command.ExecuteReaderAsync())
            {
                var retObject = new List&lt;dynamic&gt;();
                while (await dataReader.ReadAsync())
                {
                    var dataRow = new ExpandoObject() as IDictionary&lt;string, object&gt;;
                    for (var iFiled = 0; iFiled < dataReader.FieldCount; iFiled++)
                    {
                            dataRow.Add(
                            dataReader.GetName(iFiled),
                            dataReader.IsDBNull(iFiled) ? null : dataReader[iFiled] // use null instead of {}
                        );
                    }

                    retObject.Add((ExpandoObject)dataRow);
                }

                return retObject;
            }
        }
    }

2 Answer(s)
  • User Avatar
    0
    alper created
    Support Team

    looks like there's no problem with the code.

  • User Avatar
    0
    ashgadala created

    Great. Is there an alternate way to handle this by not using the using statement in every task. which is similar to Lync