Problem saving information from DropDownList to the database

1

I'm having trouble saving information in the dropdown by btn-save I have a form of registration of real estate being that the information inserted in the dropdown occurs only when I save in another page (that would be) I can not save in the database, what could it be?  follow the code ASPX.CS

     protected void btnSalvarImovel_Click(object sender, EventArgs e)
       {
        try
        {
            Imovel a = new Imovel();
            Convert.ToInt32(ddlCidade.SelectedItem.Value);
            Convert.ToInt32(ddlLocalizacao.SelectedItem.Value);
            Convert.ToInt32(ddlOperacao.SelectedItem.Value);
            Convert.ToInt32(ddlTipoImovel.SelectedItem.Value);
            a.Titulo = txtTitulo.Text;
            a.QntQuarto = Convert.ToInt32(txtQtdQuartos.Text);
            a.Obs = txtObservacao.Text;
            a.Valor = Convert.ToDouble(txtValor.Text);

            ImovelDao d = new ImovelDao();
            d.SalvarImovel(a);
            txtObservacao.Text = string.Empty;
            txtQtdQuartos.Text = string.Empty;
            txtTitulo.Text = string.Empty;
            txtValor.Text = string.Empty;
        }
        catch (Exception)
        {

            throw;
        }
    }

DAL

       public void SalvarImovel(Imovel i)
    {
        try
        {
            var command = new SqlCommand("INSERT INTO Imovel(IdCidade,IdLocal,Idtipo,IdOperacao,Titulo,Obs,QntQuarto,Valor) VALUES(@IdCidade,@IdLocal,@Idtipo,@IdOperacao,@Titulo,@Obs,@QntQuarto,@Valor)", Conexao.connection);
            Conexao.connection.Open();
            command.Parameters.AddWithValue("@IdCidade", i.IdCidade);
            command.Parameters.AddWithValue("@IdLocal", i.IdLocal);
            command.Parameters.AddWithValue("@IdTipo", i.IdTipo);
            command.Parameters.AddWithValue("@IdOperacao", i.IdOperacao);
            command.Parameters.AddWithValue("@Titulo", i.Titulo);
            command.Parameters.AddWithValue("@Obs", i.Obs);
            command.Parameters.AddWithValue("@QntQuarto", i.QntQuarto);
            command.Parameters.AddWithValue("@Valor", i.Valor);
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {

            throw new Exception("Erro ao salvar o Imóvel, atualize a página e tente novamente, se persistir o erro, contate o suporte" + ex.Message);
        }
        finally
        {
            Conexao.connection.Close();
        }
    }
}
    
asked by anonymous 28.09.2016 / 03:41

1 answer

2

What you need to do is assign the values of your DropDownList to their respective properties

            Imovel a = new Imovel();
            a.IdCidade = Convert.ToInt32(ddlCidade.SelectedItem.Value);
            a.IdLocal = Convert.ToInt32(ddlLocalizacao.SelectedItem.Value);
            a.IdOperacao = Convert.ToInt32(ddlOperacao.SelectedItem.Value);
            a.IdTipo = Convert.ToInt32(ddlTipoImovel.SelectedItem.Value);
            a.Titulo = txtTitulo.Text;
            a.QntQuarto = Convert.ToInt32(txtQtdQuartos.Text);
            a.Obs = txtObservacao.Text;
            a.Valor = Convert.ToDouble(txtValor.Text);

            ImovelDao d = new ImovelDao();
            d.SalvarImovel(a);
            txtObservacao.Text = string.Empty;
            txtQtdQuartos.Text = string.Empty;
            txtTitulo.Text = string.Empty;
            txtValor.Text = string.Empty;

Only converting to Int32 is not enough.

    
29.09.2016 / 22:20