Problem saving Dropdownlist information to the database

Asked

Viewed 190 times

1

I’m having trouble saving information in the dropdown by btn-save I have a property registration form and the information entered in the dropdown occurs only that at the time I save on another page (that would be this) I can not save in the database, what could be? follows 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();
        }
    }
}
  • 1

    What mistake are you making? Would you be able to put your code SalvarImovel

  • @Pablovargas I edited the question up there, I wanted when enrolling in the dropdown he save in Idcidade what I should do?

1 answer

2


What you need to do is assign the values of your DropDownList for 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.