error in row path of a reader () inside another reader ()

0

I have this method where I try to place a while read() into another with a different command name to retrieve data from my database, but it generates an error that says

  

{"There is already an open DataReader associated with this Command, you must close it first."}

Is my method correct or do I have to change something since I can not execute a Read() command within another?

public List<entConfiguracion> MuestraConfiguracionPredeterminada()
{
    int ValorId = 0;

    IDataParameter[] prms = new IDataParameter[]
    {
                new SqlParameter(){ParameterName="@Predeterminado",SqlDbType=SqlDbType.Bit,Value=1},
                new SqlParameter(){ParameterName="@Activo",SqlDbType=SqlDbType.Int,Value=1},
    };

    IDataReader drConfig = DB.EjecutarReaderSP("SP_ObtenerConfiguracionPredeterminada", prms);

    List<entConfiguracion> LstObtenerConfiguracion = new List<entConfiguracion>();

    while (drConfig.Read())
    {
        entConfiguracion obtener = new entConfiguracion();

        obtener.IdConfiguracion = Convert.ToInt32(drConfig["IdConfiguracion"]);
        obtener.NombreConfiguracion = drConfig["NombreConfiguracion"].ToString();
        obtener.NombreLicitacion = drConfig["NombreLic"].ToString();
        obtener.IdFormula = Convert.ToInt32(drConfig["IdFormula"]);
        ValorId = obtener.IdFormula;
        /*-------------------------------------------------------------------------------------------------------------*/
        IDataParameter[] prms1 = new IDataParameter[]
        {
                new SqlParameter(){ParameterName="@IdFormula",SqlDbType=SqlDbType.Int,Value=ValorId},
        };

        IDataReader drConfig1 = DB.EjecutarReaderSP("SP_GetVariablesCheck", prms1);

        while (drConfig1.Read())
        {
            obtener.IdVariable = Convert.ToInt16(drConfig1["IdVariable"]);
            LstObtenerConfiguracion.Add(obtener);
        }
        drConfig1.Close();
        drConfig1.Dispose();
        /*-------------------------------------------------------------------------------------------------------------*/
        obtener.TipoFuente = drConfig["TipoFuente"].ToString();
        obtener.TamanoFuente = Convert.ToInt32(drConfig["TamanoFuente"]);

        LstObtenerConfiguracion.Add(obtener);
    }
    drConfig.Close();
    drConfig.Dispose();

    return LstObtenerConfiguracion;
}
    
asked by Ivxn 07.05.2018 в 04:32
source

1 answer

0

You can not create more than 1 DataReader at a time. You will have to finish with one and then create another to do what you want.

For example you could load the list of configurations, close the reader and then look for the variables of the configurations: int ValueId = 0;

IDataParameter[] prms = new IDataParameter[]
{
            new SqlParameter(){ParameterName="@Predeterminado",SqlDbType=SqlDbType.Bit,Value=1},
            new SqlParameter(){ParameterName="@Activo",SqlDbType=SqlDbType.Int,Value=1},
};

IDataReader drConfig = DB.EjecutarReaderSP("SP_ObtenerConfiguracionPredeterminada", prms);

List<entConfiguracion> LstObtenerConfiguracion = new List<entConfiguracion>();

while (drConfig.Read())
{
    // cargas las configuraciones
    entConfiguracion obtener = new entConfiguracion();

    obtener.IdConfiguracion = Convert.ToInt32(drConfig["IdConfiguracion"]);
    obtener.NombreConfiguracion = drConfig["NombreConfiguracion"].ToString();
    obtener.NombreLicitacion = drConfig["NombreLic"].ToString();
    obtener.IdFormula = Convert.ToInt32(drConfig["IdFormula"]);
    ValorId = obtener.IdFormula;

    /*-------------------------------------------------------------------------------------------------------------*/
    obtener.TipoFuente = drConfig["TipoFuente"].ToString();
    obtener.TamanoFuente = Convert.ToInt32(drConfig["TamanoFuente"]);

    LstObtenerConfiguracion.Add(obtener);
}

// ciera el DataReader
drConfig.Dispose();

foreach(var configuracion in LstObtenerConfiguracion)
{
   // Ahora por cada conf buscas las variables abriendo otro data reader por conf
    IDataParameter[] prms1 = new IDataParameter[]
    {
            new SqlParameter(){ParameterName="@IdFormula",SqlDbType=SqlDbType.Int,Value=configuracion.FormulaId},
    };

    IDataReader drConfig1 = DB.EjecutarReaderSP("SP_GetVariablesCheck", prms1);

    while (drConfig1.Read())
    {
        configuracion.IdVariable = Convert.ToInt16(drConfig1["IdVariable"]);
        LstObtenerConfiguracion.Add(configuracion);
    }

    // cieras el data reader para crear otro luego
    drConfig1.Dispose();  
}

return LstObtenerConfiguracion;
    
answered by 07.05.2018 / 15:08
source