Estoy trabajando en C# y SQL Server 2014.
Necesito leer los datos que me está trayendo el DataReader de la base de datos. Según la propiedad HasRows
el DataReader tiene registros pero al usar el método Read()
para leerlos, el valor que devuelve Read() es false
.
public static BindingList<Cuenta> MostrarCuentas(int pIdSistema, int pIdEntidad, int pIdCartera, bool pTelResidencial, bool pTelLaboral, bool pCelPrivado, bool pCelLaboral, bool pFavorito)
{
SqlConnection con = new SqlConnection("Data Source=BGSRVDS01;Initial Catalog=ModuloDePlanificacion;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
cmd.CommandText = "sp_MostrarCuentas";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@pIdSistema", SqlDbType.Int).Value = pIdSistema;
cmd.Parameters.Add("@pIdEntidad", SqlDbType.Int).Value = pIdEntidad;
cmd.Parameters.Add("@pIdCartera", SqlDbType.Int).Value = pIdCartera;
cmd.Parameters.Add("@pTelefonoResidencial", SqlDbType.Bit).Value = pTelResidencial;
cmd.Parameters.Add("@pTelefonoLaboral", SqlDbType.Bit).Value = pTelLaboral;
cmd.Parameters.Add("@pCelularPrivado", SqlDbType.Bit).Value = pCelPrivado;
cmd.Parameters.Add("@pCelularLaboral", SqlDbType.Bit).Value = pCelLaboral;
cmd.Parameters.Add("@pFavorito", SqlDbType.Bit).Value = pFavorito;
try
{
cmd.Connection = con;
con.Open();
dr = cmd.ExecuteReader();
}
catch (Exception ex)
{
throw ex;
}
try
{
BindingList<Cuenta> Cuentas = new BindingList<Cuenta>();
while (dr.Read() && dr.HasRows) //--> necesito ingresar a este while, pero el Read() devuelve 'false'.
{
//Acá voy agarrando los datos que trae 'dr' y creo un objeto 'Cuenta'
//con el que voy armando un lista de Cuentas
}
con.Close();
return Cuentas;
}
catch (Exception ex)
{
throw ex;
}
}
Los parámetros los recibe correctamente y el stored procedure se ejecuta normalmente.
while(dr.Read() && dr.HasRows)
porwhile(dr.Read())
y dinos que pasa.