Tentativa inválida de chamar FieldCount quando o leitor está fechado

O erro acima ocorre quando tento executar um dataReader.Read nos dados recebidos do banco de dados. Eu sei que existem duas linhas lá, então não é porque realmente não existem dados.

Poderia ser o CommandBehavior.CloseConnection, causando o problema? Foi-me dito que você tinha que fazer isso logo após um ExecuteReader? Isso está correto?

        try
        {
            _connection.Open();
            using (_connection)
            {
                SqlCommand command = new SqlCommand("SELECT * FROM Structure", _connection);
                SqlDataReader dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);

                if (dataReader == null) return null;

                var newData = new List<Structure>();
                while (dataReader.Read())
                {
                    var entity = new Structure
                    {
                        Id = (int)dataReader["StructureID"],
                        Path = (string)dataReader["Path"],
                        PathLevel = (string)dataReader["PathLevel"],
                        Description = (string)dataReader["Description"]
                    };

                    newData.Add(entity);
                }
                dataReader.Close();

                return newData;
            }
        }
        catch (SqlException ex)
        {
            AddError(new ErrorModel("An SqlException error has occured whilst trying to return descendants", ErrorHelper.ErrorTypes.Critical, ex));
            return null;
        }
        catch (Exception ex)
        {
            AddError(new ErrorModel("An error has occured whilst trying to return descendants", ErrorHelper.ErrorTypes.Critical, ex));
            return null;
        }
        finally
        {
            _connection.Close();
        }
    }

Agradecemos antecipadamente por qualquer ajuda.

Clare

questionAnswers(4)

yourAnswerToTheQuestion