I have the following dropdownlist where in the first one the person chooses whether he wants rental or sale.
Thequeryinthedatabaseisbeingdoneasfollows:
publicList<Imovel>ObterCasaOperacao(stringcodigoOperacao){try{stringsql="SELECT i.CodImovel, i.Titulo, i.Foto, o.IdOp FROM Imovel i inner join Operacao o on o.IdOp = i.IdOp";
List<string> parametros = new List<string>();
if (!string.IsNullOrEmpty(codigoOperacao))
parametros.Add("o.IdOp = " + codigoOperacao);
if (parametros.Count > 0)
sql += " where " + string.Join(" and ", parametros.ToArray());
var command = new SqlCommand(sql, Conexao.connection);
Conexao.connection.Open();
var reader = command.ExecuteReader();
var imoveis = new List<Imovel>();
while (reader.Read())
{
var imovel = new Imovel();
imovel.CodImovel = Convert.ToInt32(reader["CodImovel"]);
imovel.IdOp = Convert.ToInt32(reader["IdOp"]);
imovel.Titulo = reader["Titulo"].ToString();
string[] fotos = reader["Foto"].ToString().Split(',');
if (fotos.Length > 0)
{
imovel.Foto = string.Format("/ImageHandler.ashx?h=300&w=700&code={0}&file={1}", reader["CodImovel"], fotos.First());
}
imoveis.Add(imovel);
}
return imoveis;
}
catch (Exception)
{
throw;
}
finally
{
Conexao.connection.Close();
}
}
It passes to this function in the BLL:
public List<Imovel> ObterCasaOperacao(string codigoOperacao)
{
_ImovelDao = new ImovelDao();
return _ImovelDao.ObterCasaOperacao(codigoOperacao);
}
And loads the result into this repeater
, on the search result page.
private ImovelBo _imovelBo;
protected void Page_Load(object sender, EventArgs e)
{
CarregarImoveisNoRepeater();
}
private void CarregarImoveisNoRepeater()
{
string codigoOperacao = Request.QueryString["codigoOperacao"];
_imovelBo = new ImovelBo();
rptResult.DataSource = _imovelBo.ObterCasaOperacao(codigoOperacao);
rptResult.DataBind();
}
So far so good, I know I have to do a query for each dropdownlist, the only problem is being to separate this in the search button
protected void BntPesquisar_Click(object sender, EventArgs e)
{
Response.Redirect(string.Format("~/Painel/ResultadoBusca.aspx?codigoOperacao={0}", ddlOperacao.SelectedValue));
}
I would like to know how I could put a validation, (for example, if else ou switch case ou foreach
), to be able to get each item.
For example: if I selected Operation and nothing else, it only brings the operation if you select city and nothing else it brings the city that I selected and if it selects several it back also.