Selectedvalue error of a dropdownlist in ASP.NET

Asked

Viewed 234 times

0

I have an Asp.Net form for vehicle registration, where there are DropDownList which are filled with database data. However, clicking Save returns the error

System.Exception: 'Unable to record System.Argued tofrangeexception: 'dpModel' has a Selectedvalue which is invalid because it does not exist in the list of items.

Follow the code:

public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CarregarDono();
            CarregarModelo();
        }

        BLL_Cliente bllcliente = new BLL_Cliente();
        BLL_Modelo bllmodelo = new BLL_Modelo();
        BLL_Veiculo bllveiculo = new BLL_Veiculo();
        DTO_Veiculo veiculo = new DTO_Veiculo();

        private void Limpar()
        {
            txtPlaca.Text = "";
            txtAno.Text = "";
            dpCor.Text = "";
            dpModelo.Text = "";
            dpDono.Text = "";

        }

        private void CarregarDono()
        {
            dpDono.DataSource = bllcliente.ListarClientes();
            dpDono.DataValueField = "id";
            dpDono.DataTextField = "nome";
            dpDono.DataBind();
        }

        private void CarregarModelo()
        {
            dpModelo.DataSource = bllmodelo.ListarTodosModelos();
            dpModelo.DataValueField = "id";
            dpModelo.DataTextField = "nome";
            dpModelo.DataBind();
        }

        protected void btnCadastrar_Click(object sender, EventArgs e)
        {
            try
            {
                veiculo.Placa = txtPlaca.Text;
                veiculo.Ano = txtAno.Text;
                veiculo.Cor = dpCor.SelectedValue;
                veiculo.Id_dono = int.Parse(dpDono.SelectedValue);
                veiculo.Id_modelo = int.Parse(dpModelo.SelectedValue);
                bllveiculo.InserirVeiculo(veiculo);

               string message = "Cadastro efetuado com sucesso";
                Response.Write("<script>alert('" + message + "');</script>");
                Limpar();
            }
            catch (Exception ex)
            {
                { throw new Exception("NAO FOI POSSIVEL GRAVAR" + ex); }
            }

        }


    }
  • Where the error occurs in bllInserir.InserirVeiculo()? you debugged the code and verified that you are actually receiving a warm value for the Id_Modelo (which is not null and exists in the database)?

  • In vdd it does not give error in bll it gives error in "vehicle.Id_model = int. Parse(dpModel.Selectedvalue);" but I just checked in debug and it gets a value validated yes.

2 answers

2

Turns out when you give a Postback in the application it goes through again Page_Load and recharges the DropDownList, to avoid this use the if(!IsPostBack){}, it will check if it is the first time you enter the page, ie if you have already entered the screen and performs a Postback(click a button) it will not enter the if.

your code would look something like this:

protected void Page_Load(object sender, EventArgs e)
{
   if(!IsPostBack)
   {
        CarregarDono();
        CarregarModelo();
    }
}
  • Friend I think this was one of the factors but the problem persists.

2


After some time testing and thinking I found where was the error in my Code. In the Clean() method it calls

dpCor.Text = "";

dpModel.Text = "";

dpDono.Text = "";

This made the variables empty and then did not receive a valid value.After deleting these 3 lines and placing the Postback as the friend mentioned it all worked out. Thank you very much for your attention.

Browser other questions tagged

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