I am working in a real estate portal and I have in a table inmueble
the type of property (understand "housing", "local", among others) when loading a property returns the data found in the database less the type of property which is in another table called tipoInmueble
, this value returns it as null, to explain better I will place part of the code:
In the domain layer I have the following:
public TipoInmueble tipo { get; set; }
public int id { get; set; }
public string titulo { get; set; }
These are just some fields because I think it's not worth putting them all.
In the persistence layer I have the following:
private Inmueble cargarInmuebles(SqlDataReader reader)
{
var objI = new Inmueble();
var objTipo = new TipoInmueble();
var objSubTipo = new SubTipoInmueble();
objI.id = Convert.ToInt32(reader["id"]);
objI.titulo = Convert.ToString(reader["titulo"]);
objI.contenido = Convert.ToString(reader["contenido"]);
objI.precio = Convert.ToInt32(reader["precio"]);
objI.rooms = Convert.ToInt32(reader["rooms"]);
objI.bathrooms = Convert.ToInt32(reader["bathrooms"]);
objI.garage = Convert.ToInt32(reader["garage"]);
var tipoInmueble = new tipoInmuebleMapper().obtenerPorId(objTipo.id); <-- Ésta es la línea en la cual me devuelve el valor nulo en vez de devolver el valor del id correspondiente al tipo de propiedad.
var subTipoInmueble = new subTipoInmuebleMapper().obtenerPorId(objSubTipo.id);
objI.ciudad = Convert.ToString(reader["ciudad"]);
objI.barrio = Convert.ToString(reader["barrio"]);
return objI;
}
The code of obtenerPorId()
of class tipoInmuebleMapper
public TipoInmueble obtenerPorId(int xId)
{
var param = new List<SqlParameter>();
var id = new SqlParameter();
id.ParameterName = "@id";
id.Value = xId;
param.Add(id);
var con = AbrirConexion();
var reader = select("SELECT * FROM tipoInmueble WHERE id = @id", CommandType.Text, param, con, null);
TipoInmueble t = null;
if (reader.Read())
{
t = cargarTipoInmueble(reader);
}
CerrarConexion(con);
return t;
}
This is the method that loads the reader.
public List<Inmueble> obtenerTodos()
{
List<Inmueble> listaInmuebles = new List<Inmueble>();
var param = new List<SqlParameter>();
var con = AbrirConexion();
var reader = select("SELECT * FROM inmueble", CommandType.Text, param, con, null);
while (reader.Read())
{
listaInmuebles.Add(cargarInmuebles(reader));
}
CerrarConexion(con);
return listaInmuebles;
}
I would like to know why the value nulo
or how I can get the corresponding id value obtained.
Thanks in advance and feel free to request any additional information.