Hello I want to fill a list with objects after consulting a bd,
List<EUbigeo> lista = new List<EUbigeo>();
String query = "select * from ubigeo where estado='A'";
comando.Connection = conexion.openConexion();
comando.CommandText = query;
leer = comando.ExecuteReader();
EUbigeo ubigeo = new EUbigeo();
try {
while(leer.Read())
{
ubigeo.Departamento = Convert.ToString(leer[0]);
ubigeo.Provincia = Convert.ToString(leer[1]);
ubigeo.Distrito = Convert.ToString(leer[2]);
ubigeo.Nombre= Convert.ToString(leer[3]);
ubigeo.Estado = Convert.ToChar(leer[4]);
lista.Add(ubigeo);
}
}
catch (Exception e)
{
MessageBox.Show("Error: " + e.Message);
}
return lista;
now it turns out, that the list of objects is full but of the same object, that is if the last object is: ubigeo [departamento5, provincia5, distrito5, A] ... the complete list is full of that same object.
It has already been tested in the following way with the same result
String query = "select * from ubigeo where estado='A'";
comando.Connection = conexion.openConexion();
comando.CommandText = query;
leer = comando.ExecuteReader();
try {
while (leer.Read())
{
lista.Add(new EUbigeo()
{
Departamento = Convert.ToString(leer[0]),
Provincia = Convert.ToString(leer[1]),
Distrito = Convert.ToString(leer[2]),
Nombre = Convert.ToString(leer[3]),
Estado = Convert.ToChar(leer[4])
});
}
}
catch (Exception e)
{
MessageBox.Show("Error: " + e.Message);
}
return lista;