Error in SelectedValue of a dropdownlist in ASP.NET

0

I have a form in Asp.Net for registration of vehicles, where there are DropDownList that are filled with data from the bank. However, when you click on Write it returns the error

  

System.Exception: 'IT WAS NOT POSSIBLE TO RECORD   System.ArgumentOutOfRangeException: 'dpModel' has a SelectedValue   which is invalid because it does not exist in the list of items.

Follow the code below:

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); }
            }

        }


    }
    
asked by anonymous 19.09.2018 / 02:50

2 answers

2

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

  

dpCor.Text="";

     

dpModelo.Text="";

     

dpDono.Text="";

This made the variables empty and then did not receive a valid value. After deleting these 3 lines and putting the PostBack as the friend mentioned it worked out fine . Thank you very much for the attention of everyone.

    
20.09.2018 / 11:15
2

It happens that by giving a PostBack in the application it goes back to Page_Load and reloads DropDownList , to avoid this use if(!IsPostBack){} , it will check if this is the first time you enter the page, that is, if you have already entered the screen and perform a PostBack (click a button) it will not enter if .

Your code would look something like this:

protected void Page_Load(object sender, EventArgs e)
{
   if(!IsPostBack)
   {
        CarregarDono();
        CarregarModelo();
    }
}
    
19.09.2018 / 03:32